function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raksha Tiwari 8Raksha Tiwari 8 

Update account status according to status of all related contacts

Hi All,

I have a requirement in which account status should be updated if its all related contacts have same status. I have three status values in contact, 'New', 'In progress' and 'Cancelled'. Suppose an account A has three related contacts C1, C2 & C3. If the status of all three contacts is 'New', then account's status should be 'New'. Can anyone help me out on this? 

Regards,
Raksha
Amit Chaudhary 8Amit Chaudhary 8
Hi
I will suggest you to start learning Trigger from Trailhead . (https://trailhead.salesforce.com/modules/apex_triggers)

Start Writing your code and post the issue if you got any.

Your trigger will simple. Try to follow below step
1) Write Trigger on Contact
2) Create on Set and add accountID in same
3) then Query all Account and contact like below
List<Account> lstAcc = [select id,Status__c ,(select id,name,Status__c from contacts) from Account where id in :SetACCID ];
4) Then add your custom logic to check contact status and update account status.
for (Account acc: listAcc)
{
   List<Contact> lstCont = acc.contacts;
   for(Contact cont: lstCont)
   { 
       // Add your logic here
   }
   // Update Account here is required.
}

Let us know if this will help you



 
AvaneeshAvaneesh
Hi Raksha , 

here is your trigger it is working fine in my org 
In this trigger i create a picklist in contact name : Contact_Status__c (api name)
and another picklist in account object : Account_Status__c (api name)

 
trigger ContactTriggerForStatus on Contact (after insert ,after update) {
    
    set<id> allRelatedidset = new set<id>();
    Map<id,String> RelatedMap = new Map<id,String>();
    Set<id> onlyForLogicSet = new set<id>();
    List<Account> accountList = new List<Account>();
    for(contact con:trigger.new){
        if(con.Contact_Status__c !=NULL && con.AccountId !=NULL){
            allRelatedidset.add(con.AccountId);
        }
    }
    for(contact con:[select id,Contact_Status__c,accountid from contact where accountid IN :allRelatedidset]){
        if(RelatedMap.containsKey(con.AccountId)){
            if(!RelatedMap.get(con.AccountId).equals(con.Contact_Status__c)){
                onlyForLogicSet.add(con.AccountId);
               RelatedMap.remove(con.AccountId);
            }
        }else if(onlyForLogicSet.contains(con.AccountId)){            
        RelatedMap.put(con.AccountId,con.Contact_Status__c);
            System.debug('RelatedMap===>'+RelatedMap);            
        }else{                   
        RelatedMap.put(con.AccountId,con.Contact_Status__c);
        }
    }
    for(contact con:trigger.new){
        if(con.Contact_Status__c !=NULL && relatedmap.containsKey(con.AccountId)){
            if(RelatedMap.get(con.AccountId).equals(con.Contact_Status__c)){
                Account acc = new Account();
                acc.Id = con.AccountId;
                acc.Account_Status__c = con.Contact_Status__c;
                accountList.add(acc);
            }
        }   
    }
    System.debug('!!!!!!!!!!!!!!!!'+accountList);
    if(accountList.size()>0){
        update accountList;
    }    
}

if this was helpful please mark as best answer else let me know 

Thank you
Avaneesh Singh