0

Hi i have written trigger in which SC_Problem_Management__c is parent object and SC_Problem_Case_Link__c is child object having master detail relationship. I want to update status field of parent object to awaiting deployment when all child records status having value as 'Tested'.

trigger updatestatus on SC_Problem_Case_Link__c(after update) {
    List<SC_Problem_Management__c> parentObjList = new List<SC_Problem_Management__c>();
    List<SC_Problem_Management__c> parentObjListToBeUpdated = new List<SC_Problem_Management__c>();
    public List<Id> listIds = new List<Id>();
    for (SC_Problem_Case_Link__c childObj : Trigger.new) {
        listIds.add(childObj.SC_Problem_Management__c);
    }
    parentObjList = [SELECT Id,Status__c, (SELECT ID, Status__c FROM SC_Problem_Case_Link__r) FROM SC_Problem_Management__c WHERE ID IN :listIds];
    Boolean checkApproval = TRUE;
    for (SC_Problem_Management__c bid : parentObjList) {
        for (SC_Problem_Case_Link__c bsaa : bid.SC_Problem_Case_Link__r) {
            if (bsaa.Status__c == 'Tested') {
                checkApproval = TRUE;
            } else {
                checkApproval = FALSE;
            }
        }
        if (checkApproval == TRUE) {
            bid.Status__c = 'Awaiting Deployment';
        }
        parentObjListToBeUpdated.add(bid);
    }
    if (parentObjListToBeUpdated.size() > 0) {
        update parentObjListToBeUpdated;
    }
}

I am getting error while saving the trigger. "Error Error: Compile Error: Status__c,(SELECT ID, Status__c FROM SC_Problem_Case_Link__r) FROM SC_Problem_Management__c ^ ERROR at Row:1:Column:48 Didn't understand relationship 'SC_Problem_Case_Link__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 9 column 21"

Please guide on this issue.

  • Open the Master Detail Field, and then check Child Relationship Name. Your inner query table would be Child_Relationship_Name__r – Pranay Jaiswal Feb 24 at 17:31
  • 1
    Your logic also contains an issue. If your last child record is "Tested", then your parent record will be marked as "Awaiting Deployment", even if there are other child records that aren't "Tested". – Derek F Feb 24 at 17:36
  • 2
    Any reason you cannot use Rollup Summary fields since you have a Master-Detail relationship? You can create a rollup summary field for count of all child records and another field to see the count of child records with status as Tested. When both counts match, a trigger / WF Rule on the parent can update the status on the parent record. – Aayush K Feb 24 at 17:58
  • @pranay child relationship name is correct but don't know why it is showing me an error – Admin Feb 24 at 18:10
  • 1
    This is because you are doing it in an After Update trigger. This should be done on a Before Update trigger. – Aayush K Feb 25 at 4:33
0

Moving the comment to the answer section so that the question can be closed.

You can use Rollup Summary fields since you have a Master-Detail relationship.

You can create a rollup summary field for count of all child records and another field to see the count of child records with status as Tested. When both counts match, a trigger (before update) / WF Rule on the parent can update the status on the parent record.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.