-1

I have a custom object called child_object__c with a lookup into Parent_Object__c records. When Parent_Object__c Type field value Primary to Secondary is changed, I need to field update all the child_object__c (child) records with the status value Pending to confirmed. Anybody have a code example for this?

1
  • Here we can help in fixing some bug you are facing, not in writing code from scratch. Apr 17, 2017 at 9:00

4 Answers 4

2

You can use process builder for this simple scenario. No need of using trigger for this.

Create a process builder on the parent object. set your desirable criteria. and select action- 'update records'. select the child object and update the status field from pending to confirmed.

Create a trigger and then call the helper class from it. [best practise] Here is the code:

trigger updatechildrec on Parentobject(after update){
    if(trigger.isupdate && trigger.isafter){
        parentobjectClass.updatechildrecMethod(trigger.new, trigger.oldMap);
    }
}


public class parentobjectClass{

    public static void updatechildrecMethod(List<parentobject> records, Map<Id, parentobject> recordsMap){
        set<id> parentobjId = new set<id>();
        for(Opportunity_Service_Line_Item__c item : records)
        {
            if(item.Type =='secondary' && recordsMap.get(item.Id).type == 'primary'){
                parentobjId.add(item.id);

            }       
        }
        list<childobject> childobjList = [select id,status from childobject where ParentFieldRelationid in: parentobjId];
        list<childobject> toUpdateList = new list<childobject>();
        for(childobject eachChild : childobjList){
            eachChild.status = 'confirmed';
            toUpdateList.add(eachChild);
        }
        if(toUpdateList.size() > 0){
            update toUpdateList; //dml on childobject
        }
    }
}
3
  • Thanks for Your suggestion,but my requirement is Trigger
    – Sfdcprgmr
    Apr 17, 2017 at 7:54
  • i have updated my answer. please check the code
    – Anujit Das
    Apr 17, 2017 at 8:19
  • did the above code solve your problem?
    – Anujit Das
    Apr 18, 2017 at 11:58
1
trigger UpdateChildType on Parent_Object__c(After Update){   

  public set<Id> setQualiParentId = new set<Id>();
  public List<Child_Object__c> chldObListToUpdate = new List<Child_Object__c>();

  if(Trigger.isUpdate && Trigger.isAfter){
    for(Parent_Object__c parntObjIst: Trigger.new){
        if(parntObjIst.Type__c == 'Secondary'){
            setQualiParentId.add(parntObjIst.Id);            
        }
    }
    chldObListToUpdate = new List<Child_Object__c>();            
    if(setQualiParentId.size() > 0){
        for(Child_Object__c chObje:[SELECT Id,Child_Type__c FROM Child_Object__c WHERE Parent_Object__c in: setQualiParentId]){
           Child_Object__c chldTemp = new Child_Object__c();
           chldTemp.Child_Type__c = 'Confirmed';
           chldTemp.Id = chObje.Id;
           chldObListToUpdate.add(chldTemp);
        }        
    }
    if(chldObListToUpdate.size() > 0){
        update chldObListToUpdate;
    }    
  }
}
1

@Sfdcprog can you please check below code:

trigger Conrecupdate on Account (After update) {

              List<id> accid = new List<id>();
               List<id> accid1 = new List<id>();

                  for(account a:Trigger.new)
                  {
                    accid.add(a.id);
                  }

                  List<account> acclst = [select id,name,type,(select id,firstname,lastname,email,Level__c,accountid from contacts where accountid=:accid) from account];
                  List<contact> conlst = new List<contact>();

              for(account aa:acclst)
              {
                for(contact c:aa.contacts)
                {
                  if(aa.Type =='Prospect')
                  {
                    c.Level__c = 'Primary';
                  }else if(aa.type == 'Installation Partner')
                  {
                    c.Level__c = 'Secondary';
                  }

                  else if(aa.type == 'Other')
                  {
                    c.Level__c = '';
                  }
                  conlst.add(c);
                }
              }
              update conlst;

            }
0

Hi you can check in this way,

trigger ConAllUpdate on Account (After update) {

  List<id> accid = new List<id>();
  for(account a:Trigger.new)
  {
    if(a.Type__c =='Secondary')
    {
      accid.add(a.id);
    }
    if(!accid.isempty())
    {
      List<Contact> cc = [select id,name,accountid,status__c from Contact where accountid=:accid];
      if(!cc.isempty())
      {
        for(contact c:cc)
        {
          c.status__c='Completed';
        }
        update cc;
      }
    }
  }
}

You must log in to answer this question.

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