You need to sign in to do that
Don't have an account?
himanshu huske 7
trigger_on Contact
Both Account and Contact has a picklist field Status_c with values Open,Close,InProgress
account record has 3 child record in contact
if all 3 contact has picklist value = 'close', then account picklist value will get updated to 'close'.
account record has 3 child record in contact
if all 3 contact has picklist value = 'close', then account picklist value will get updated to 'close'.
The code should be something in the lines of this. However, the best way is to have this logic in a separate handler class and then invoke that class within this trigger. You can find many examples online something like this https://developer.salesforce.com/forums/?id=906F00000009EfuIAE
Please mark it as the best answer if this helps.
Cheers!
Sathish
Please try the below trigger
trigger AccountStatucUpdate on Contact (After insert, After update) {
Set<Id> accIds = new Set<Id>();
List<Account> accListtoUpdate = new List<Account>();
if (Trigger.isInsert)
{
for(Contact con : Trigger.new)
{
if(con.AccountId != null && con.status__c == 'Close')
{
accIds.add(con.AccountId);
}
}
}
else if(Trigger.isUpdate)
{
for(Contact c : Trigger.new)
{
if(c.status__c == 'Close' && c.accountId != null && trigger.OldMap.get(c.Id).Status__c != trigger.newMap.get(c.Id).Status__c)
{
accIds.add(c.AccountId);
}
}
}
if(!accIds.IsEmpty())
{
List<Account> accList = [Select id,(select id, status__c from Contacts) from Account where id IN: accIds];
for(Account acc : accList)
{
Integer unclosedCons = 0;
for(Contact cont : acc.Contacts)
{
if(cont.status__c != 'Close')
{
unclosedCons++;
}
}
if(unclosedCons == 0)
{
acc.Status__c = 'Close';
accListtoUpdate.add(acc);
}
}
}
if(!accListtoUpdate.isEmpty())
update accListtoUpdate;
}
Thanks,
Maharajan.C
You can use the below code:
Thanks
You can use the below code:
<------ Apex class--->>>>>
<<<---- Trigger------>>>>
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi