Scenario

1. Create lookup field in contact object of contact, name as "Parent Contact".
2. Create picklist in contact name as Status with below values: a. Pending b. Running c. Completed
3. If someone change the status of contact then all parent contact should be updated with the same status.
Example : Contact A Contact B Contact C Contact D

Contact C is parent of Contact D, Contact B is parent of Contact C, Contact A is parent of Contact B

Contact A (Status) = Running
Contact B (Status) = Pending
Contact C (Status) = Running
And if we change the status of Contact D with "Completed" then all parent should be changed with "Completed" Status.

public class StackEx {
    public Static void updaterec(Set<String> nstr, String str){
        
         list<contact> ConListNew=new list<contact>();
         list<contact> ContList =[select id,Parent_Contact__c,Status__c from contact where id in: nstr];
         for(contact con : ContList) {
              con.Status__c=str;
              ConListNew.add(con);
          }
        if(ConListNew.size()>0){
              update ConListNew;      
    }
}

trigger StackEx on Contact (after insert, after update) {
    Set<String> prntSet=new Set<String>();
    string st;
    if(trigger.isInsert||trigger.isUpdate){
        for(contact cc:Trigger.new){
            if(cc.Parent_Contact__c !=null){
            prntSet.add(cc.Parent_Contact__c);
                st=cc.Status__c;
            }
         }
    }
    if(prntSet.size()>0){
        StackEx.updaterec(prntSet, st);
    }

}

Comments

Popular posts from this blog

Page Layouts

SOQL Scenario-1