You need to sign in to do that
Don't have an account?
SFDC Guest
Apex trigger to update Contact and its Cases When Account is Updated.
Hi All,
I need a trigger to update Contact Description and Contact related Case object Description whenever Account Description is updated.
Below trigger working fine for updating Contact Description when Account Description is updated. But I need to update Case records of Contact as well.
Here Account is ---> Parent,
Contact is ---> Child,
Case is ---> grand child
trigger ContactDescriptionUpdate on Account (after update) {
Map < Id, Account > mapAccount = new Map < Id, Account >();
List<Contact> listContact = new List<Contact>();
for(Account acct : trigger.new)
mapAccount.put(acct.Id, acct);
listContact = [ SELECT Description, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
if ( listContact.size() > 0 ) {
for ( Contact con : listContact ) {
con.Description = mapAccount.get(con.AccountId).Description;
}
update listContact;
}
}
Thanks in advance.
I need a trigger to update Contact Description and Contact related Case object Description whenever Account Description is updated.
Below trigger working fine for updating Contact Description when Account Description is updated. But I need to update Case records of Contact as well.
Here Account is ---> Parent,
Contact is ---> Child,
Case is ---> grand child
trigger ContactDescriptionUpdate on Account (after update) {
Map < Id, Account > mapAccount = new Map < Id, Account >();
List<Contact> listContact = new List<Contact>();
for(Account acct : trigger.new)
mapAccount.put(acct.Id, acct);
listContact = [ SELECT Description, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
if ( listContact.size() > 0 ) {
for ( Contact con : listContact ) {
con.Description = mapAccount.get(con.AccountId).Description;
}
update listContact;
}
}
Thanks in advance.
Please try this. Hope this will work for you.
trigger AccTrig on Account (after update) {
UpdateCls.updateContactLeadDescription(trigger.new);
}
public class UpdateCls {
public static void updateContactLeadDescription(List<Account> accList)
{
Map <Id, Account> mapAccount = new Map <Id, Account>();
List<Contact> conList = new List<Contact>();
List<Case> caseList = new List<Case>();
for(Account acct : accList)
mapAccount.put(acct.Id, acct);
conList = [ SELECT id,Description, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
if ( conList.size() > 0 ) {
for ( Contact con : conList ) {
con.Description = mapAccount.get(con.AccountId).Description;
}
update conList;
}
caseList = [SELECT id,Description,ContactId,AccountId FROM Case WHERE ContactId IN:conList];
if ( caseList.size() > 0 ) {
for (Case caseObj : caseList ) {
caseObj.Description = mapAccount.get(caseObj.AccountId).Description;
}
update caseList;
}
}
}
Thank You
Ajay Dubedi
All Answers
Getting error as :
Compile Error: Method does not exist or incorrect signature: void keySet() from the type List<Contact> at line 17 column 102
Please try this. Hope this will work for you.
trigger AccTrig on Account (after update) {
UpdateCls.updateContactLeadDescription(trigger.new);
}
public class UpdateCls {
public static void updateContactLeadDescription(List<Account> accList)
{
Map <Id, Account> mapAccount = new Map <Id, Account>();
List<Contact> conList = new List<Contact>();
List<Case> caseList = new List<Case>();
for(Account acct : accList)
mapAccount.put(acct.Id, acct);
conList = [ SELECT id,Description, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
if ( conList.size() > 0 ) {
for ( Contact con : conList ) {
con.Description = mapAccount.get(con.AccountId).Description;
}
update conList;
}
caseList = [SELECT id,Description,ContactId,AccountId FROM Case WHERE ContactId IN:conList];
if ( caseList.size() > 0 ) {
for (Case caseObj : caseList ) {
caseObj.Description = mapAccount.get(caseObj.AccountId).Description;
}
update caseList;
}
}
}
Thank You
Ajay Dubedi