You need to sign in to do that
Don't have an account?

Trigger not updating from Account to Contact
Hi guys,
I have this trigger on the account and it is not updating at all on contacts. there are no errors either. I tried updating fields on the account and it did not update them on contacts.
Thanks in advance!!
I have this trigger on the account and it is not updating at all on contacts. there are no errors either. I tried updating fields on the account and it did not update them on contacts.
trigger contactAccountTrigger on Account (after update) { Set<Id> accountIds = new Set<Id>(); List<Contact> contactList1 = new List<Contact>(); for(Account acc: Trigger.new){ Account oldAccount = Trigger.oldMap.get(acc.ID); If(acc.Contacts.size()>0){ accountIds.add(acc.id); } }//End of Loop If(accountIds.size()>0){ Map<Id,Account> mapAccount = new Map<Id,Account> ([select id,Tier__c,Type, (select id,Account_Tier__c,Account_Type__c from contacts ) from account where id in :accountIds ]); List<Contact> contactList = new List<Contact>(); for(account acc :Trigger.new) { //Account oldAccount = Trigger.oldMap.get(acc.ID); If(mapAccount.containsKey(acc.id)) { Account accObj = mapAccount.get(acc.id); List<Contact> lstCont = accObj.contacts; for(contact c:lstCont) { c.Account_Tier__c=acc.Tier__c; c.Account_Type__c = acc.Type; contactList.add(c); }//end of for loop System.debug('2nd Loop- Inside'); } } //end of Acc1 for loop if(contactList.size()>0) { update contactList1; } }// End of accountIds.size() }//End of trigger
Thanks in advance!!
Try the below trigger:
trigger contactAccountTrigger on Account (after update) {
Set<Id> accountIds = new Set<Id>();
for(Account acc: [SELECT Id,(SELECT Id,checkbox__c FROM Contacts) FROM Account WHERE Id in: Trigger.new]]){
If(acc.Contacts.size()>0){
accountIds.add(acc.id);
}
}//End of Loop
If(accountIds.size()>0){
List<Contact> contactList = [select id,Tier__c,Type, (select id,Account_Tier__c,Account_Type__c from contact where ID IN: accountIds];
for(contact c:contactList)
{
c.Account_Tier__c = Trigger.newMap.get(c.AccountId).Tier__c;
c.Account_Type__c = Trigger.newMap.get(c.AccountId).Type;
}//end of for loop
if(contactList.size()>0)
{
update contactList;
}
}// End of accountIds.size()
}//End of trigger
Thanks,
Maharajan.C
Change
update contactList1;
to
update contactList;
Try the following code, it may be helpful for you:
trigger contactAccountTrigger on Account (after update) {
Set<Id> accountIdSet = new Set<Id>();
List<Contact> contactList= new List<Contact>();
List<Contact> contactListNew= new List<Contact>();
Map<Id,List<Contact>> accountIdVScontactMap = new Map<Id,List<Contact>>();
for(Account acc: Trigger.new){
accountIdSet.add(acc.Id);
}
If(accountIdSet.size()>0){
contactList=[select Id,Account_Tier__c,Account_Type__c,AccountId from contacts WHERE AccountId IN:accountIdSet LIMIT 10000];
if(contactList.size()>0){
for(Contact con:contactList){
if(con.AccountId!=null){
if(!accountIdVScontactMap.containsKey(con.AccountId)){
accountIdVScontactMap.put(con.AccountId,new List<Contact>());
accountIdVScontactMap.get(con.AccountId).add(con);
}
else{
accountIdVScontactMap.get(con.AccountId).add(con);
}
}
}
}
}
if(accountIdVScontactMap.size()>0){
for(Account acc :Trigger.new){
if(accountIdVScontactMap.containsKey(acc.id)){
List<Contact> conList = accountIdVScontactMap.get(acc.id);
for(contact con:conList){
con.Account_Tier__c=acc.Tier__c;
con.Account_Type__c = acc.Type;
contactListNew.add(c);
}
}
}
}
if(contactListNew.size()>0){
update contactListNew;
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi