0

I'm trying to update parent object field based on all the records available in child object.

I have two objects A & B. A is parent and B is child, A is having a Pick list field 'Status', B is having check box field 'Completed'

Now 'Status'should be updated when its all the child records are checked 'completed'.

Here is the code i've written

 trigger updateParent on Project__c (after insert, after update) {
    boolean check = false; 
    Plan__c parentObj;
    List<Plan__c> somePlan; 

    List<Project__c> childsObj = new List<Project__c>([SELECT Id, Completed__c FROM Project__c WHERE PlanR__c = 'a0228000007O8Kr']);
    //childsObj = [SELECT Id, Completed__c FROM Project__c WHERE PlanR__c = 'a0228000007O8Kr'] ;

        for(Project__c child : childsObj){
            if(child.Completed__c == false){
                check = true; 
            }
        }

        if(check == false){
            parentObj = new Plan__c();
            parentObj.id = 'a0228000007O8Kr';
            parentObj.Status__c = 'Completed'; 
            update parentObj;
        }
    }

Please suggest an idea or share if any trigger available.

thanks in advance.

7
  • what you have done so far for this? we are here to help you but It will be great if you can show some effort prior to ask a question on SE.
    – Himanshu
    Commented Jan 7, 2016 at 6:17
  • I have done hard coding for this, getting multiple errors while bulkifying it...
    – venky
    Commented Jan 7, 2016 at 6:19
  • Please share your code, so people can suggest fixes. People won't likely write a trigger for you but will gladly offer suggestions on how to improve what you wrote. Commented Jan 7, 2016 at 6:23
  • I have added the code in the question, trying to bulkfy this..
    – venky
    Commented Jan 7, 2016 at 6:24
  • 1
    You can just do this with rollups and a formula if you are clever. If it's not M/D use DLRS.
    – Adrian Larson
    Commented Jan 7, 2016 at 6:39

1 Answer 1

4

here is a trigger according to your requirement !

here Object A = Parent__c & Object B = Child__c in my case

Trigger :

 trigger childTrigger on Child__c (after insert,after update) {
    childTriggerHandler objChildTrigHand = new childTriggerHandler();
    if(Trigger.isAfter) {
        if(Trigger.isInsert ) {
            objChildTrigHand.UpdateFields(Trigger.new);
        }
        if(Trigger.isUpdate ) {
            objChildTrigHand.UpdateFields(Trigger.new);
        }
    } 
}

Trigger handler :

/*
* This class Update status Fields of parent object in salesforce.
*/

public with sharing class childTriggerHandler {
    public void UpdateFields(List<Child__c> newList) {
        List<Parent__c> updateList = new List<Parent__c>();
        Map<id,List<Child__c>> mapParentIDAndChild = new Map<id,List<Child__c>>();
        boolean flag;
        set<Id> parenetTrue = new set<Id>();
        for(Child__c objChild : newList) {
            parenetTrue.add(objChild.Parent__c);
        }
        for(Child__c objChild : [select id , Completed__c , Parent__c from Child__c where Parent__c IN: parenetTrue] ){
            system.debug('<<<<<<objChild '+objChild );
            if(mapParentIDAndChild.containsKey(objChild.Parent__c)) {
                system.debug('<<<<<<in if 1st ');
                mapParentIDAndChild.get(objChild.Parent__c).add(objChild);
                system.debug('<<<<<<in if 1st mapParentIDAndChild '+mapParentIDAndChild);
            } else {
                system.debug('<<<<<<in else 1st ');
                mapParentIDAndChild.put(objChild.Parent__c,new List<Child__c>{objChild});
                system.debug('<<<<<<in else 1st mapParentIDAndChild '+mapParentIDAndChild);
            }
        }
        for(Id objId : mapParentIDAndChild.keySet()) {
            flag = true;
            for(Child__c objChild : mapParentIDAndChild.get(objId)) {
                if(objChild.Completed__c == false){
                    flag = false;
                    break;
                }
            }
            if(flag == true) {
                updateList.add(new Parent__c(Id = objId , Status__c = 'Updated'));
            } else {
                updateList.add(new Parent__c(Id = objId , Status__c = 'In-progress'));
            }
        }
        if(!updateList.isEmpty()) {
            update updateList;
        }

    }// end of UpdateFields
}// end of childTriggerHandler

Not the answer you're looking for? Browse other questions tagged .