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
Saoni PaitandiSaoni Paitandi 

Help in Trigger(Parent field updation based on chield record)

Hi All !!I have Written the following code to update Account custom field if Contact Status field is Finish,But I wanted to do if all the assosiated contact status finish then only it should be end.
Code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
trigger UpdateAccountStatus on Contact (after insert,after update) {
List<ID> actListID=new List<Id>();
for(Contact con:Trigger.new)
{
if(con.Status__c=='Finish'&& con.AccountID!=Null){
actListID.add(con.AccountID);
}
}
List<Account> acc=[Select Id,Name,Status__c from Account where Id=:actListID];
for(Account a:acc)
a.Status__c = 'End';

update acc;
}
Best Answer chosen by Saoni Paitandi
sfdcMonkey.comsfdcMonkey.com
sure, please check code comments :
trigger UpdateAccountStatus on Contact (after insert,after update) {
 // create a list of account Id  
 List<ID> actListID = new List<Id>();
  // play a for loop on trigger.new (contact List) and check if contact account Id != null and 
  // contact status = 'finish'  
  for(Contact con:Trigger.new){
     if(con.Status__c == 'Finish'&& con.AccountID != Null){
       actListID.add(con.AccountID);
     }
  }

  
  // create a list of account to update account status  
   List<Account> lstUpdate = new List<Account>() ;
  
 // play a for loop on account and query all child records of account object where Id in actListID (trigger.New account id's) 
  for(account acc : [select id,Status__c,(select id,Status__c from contacts) from account where id IN : actListID]){
    // take a boolean flag to check if all contact Status__c finish or NOT 
	 boolean bStatusEnd = true;
    // play a for loop on account realted contacts.. 
	// and check if status__c != Finish	then make boolean flag as false
	for(contact cc : acc.contacts){
	  if(cc.Status__c != 'Finish'){
	    bStatusEnd = false;
	  }
	}
	
  // if bStatusEnd = true, means all contact status__c fields have 'Finish' value then update the account status 
  // and add account in list to update   
	if(bStatusEnd){
	   acc.Status__c = 'End';
	   lstUpdate.add(acc);
	}
	
  }
  
  //   check if lstUpdate have more then 0 records then update the account list
  if(lstUpdate.size() > 0){
    update lstUpdate;
   }
}
hope it will helps you,
 

All Answers

sfdcMonkey.comsfdcMonkey.com
Hi saoni, use following trigger code :
trigger UpdateAccountStatus on Contact (after insert,after update) {
 List<ID> actListID=new List<Id>();
  for(Contact con:Trigger.new){
     if(con.Status__c == 'Finish'&& con.AccountID != Null){
       actListID.add(con.AccountID);
     }
  }

  
   List<Account> lstUpdate = new List<Account>() ;
  for(account acc : [select id,Status__c,(select id,Status__c from contacts) from account where id IN : actListID]){
     boolean bStatusEnd = true;
	for(contact cc : acc.contacts){
	    if(cc.Status__c != 'Finish'){
		   bStatusEnd = false;
		}
	}
	
	if(bStatusEnd){
	   acc.Status__c = 'End';
	   lstUpdate.add(acc);
	}
	
  }
  
  if(lstUpdate.size() > 0){
    update lstUpdate;
   }
}

Thank, let us know if it helps you  and close your query with best answer if it helps you
 
Saoni PaitandiSaoni Paitandi
Hi {!Piyush_soni__c}!
Thanks a lot for replying but could you please explain your code a bit!!
sfdcMonkey.comsfdcMonkey.com
sure, please check code comments :
trigger UpdateAccountStatus on Contact (after insert,after update) {
 // create a list of account Id  
 List<ID> actListID = new List<Id>();
  // play a for loop on trigger.new (contact List) and check if contact account Id != null and 
  // contact status = 'finish'  
  for(Contact con:Trigger.new){
     if(con.Status__c == 'Finish'&& con.AccountID != Null){
       actListID.add(con.AccountID);
     }
  }

  
  // create a list of account to update account status  
   List<Account> lstUpdate = new List<Account>() ;
  
 // play a for loop on account and query all child records of account object where Id in actListID (trigger.New account id's) 
  for(account acc : [select id,Status__c,(select id,Status__c from contacts) from account where id IN : actListID]){
    // take a boolean flag to check if all contact Status__c finish or NOT 
	 boolean bStatusEnd = true;
    // play a for loop on account realted contacts.. 
	// and check if status__c != Finish	then make boolean flag as false
	for(contact cc : acc.contacts){
	  if(cc.Status__c != 'Finish'){
	    bStatusEnd = false;
	  }
	}
	
  // if bStatusEnd = true, means all contact status__c fields have 'Finish' value then update the account status 
  // and add account in list to update   
	if(bStatusEnd){
	   acc.Status__c = 'End';
	   lstUpdate.add(acc);
	}
	
  }
  
  //   check if lstUpdate have more then 0 records then update the account list
  if(lstUpdate.size() > 0){
    update lstUpdate;
   }
}
hope it will helps you,
 
This was selected as the best answer