You need to sign in to do that
Don't have an account?
![Deepak Singh 116 Deepak Singh 116](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Need a trigger for update contact picklist field when account status updated
I have two status picklist field one at contact and one at account.Both the picklist have the same value (Active,Inactive,closed).Now i want when i update the status on account then the contactstatus is automatically update as account.
Please try this below code. This code is an easy and simple way to understand for new Salesforce Developer.
//Apex Helper Class//
public class AccountUpdateContactHelperClass {
public static void updateContact(List<Account> accList){
Set<Id> setOfAccIds = new Set<Id>();
System.debug('Account status'+accList);
for(Account ac:accList){
if(ac.Id!=null){
setOfAccIds.add(ac.Id);
}
}
List<Contact> conList = new List<Contact>();
conList = [SELECT ID ,LastName,Status__c FROM Contact WHERE AccountId IN: setOfAccIds];
List<Contact> conList1 = new List<Contact>();
for(Account a:accList){
System.debug('Account status'+a.Status__c);
for(Contact con:conList){
System.debug('Cont status'+con.Status__c);
con.AccountId =a.Id;
con.Status__c = a.Status__c;
conList1.add(con);
}
}
if(conList1.size()>0){
update conList1;
}
}
}
//Trigger Class//
trigger AccountTrigger on Account (after update) {
AccountUpdateContactHelperClass.updateContact(Trigger.New);
}
Please mark it as best if you find it helpful.
Thank You
Ajay Dubedi
All Answers
Please try this below code. This code is an easy and simple way to understand for new Salesforce Developer.
//Apex Helper Class//
public class AccountUpdateContactHelperClass {
public static void updateContact(List<Account> accList){
Set<Id> setOfAccIds = new Set<Id>();
System.debug('Account status'+accList);
for(Account ac:accList){
if(ac.Id!=null){
setOfAccIds.add(ac.Id);
}
}
List<Contact> conList = new List<Contact>();
conList = [SELECT ID ,LastName,Status__c FROM Contact WHERE AccountId IN: setOfAccIds];
List<Contact> conList1 = new List<Contact>();
for(Account a:accList){
System.debug('Account status'+a.Status__c);
for(Contact con:conList){
System.debug('Cont status'+con.Status__c);
con.AccountId =a.Id;
con.Status__c = a.Status__c;
conList1.add(con);
}
}
if(conList1.size()>0){
update conList1;
}
}
}
//Trigger Class//
trigger AccountTrigger on Account (after update) {
AccountUpdateContactHelperClass.updateContact(Trigger.New);
}
Please mark it as best if you find it helpful.
Thank You
Ajay Dubedi
Thanks Ajay it works.
I need a logic less trigger can you guide me how to achiev using you above trigger.