I am writing the trigger for When I update the contact record description value of an Account, and if other contacts description value related to that Account is matched. Then update the Account description value with the contact description.
Example: There is one Account record: Account1 There are 3 Contact records for this Account: Contact1, Contact2, Contact3.
If I update Contact1 description as 'test desc', trigger should check if remaining contacts Contact2 and Contact3 are having same description value, if yes please update Account description with Contact1 description.
How to check whether contacts also have same description of same account in trigger. please help. Thanks in advance.
Below trigger works if we update the contact descritpion then Account description will be updated.
trigger UpdateDescription on Contact (after insert, after update) {
Map<ID, Account> parentAccts = new Map<ID, Account>();
List<Id> listAccIds = new List<Id>();
for (Contact contObj : Trigger.new) {
listAccIds.add(contObj.AccountId);
}
parentAccts = new Map<Id, Account>([SELECT id, Description,(SELECT ID, Description FROM Contacts) FROM Account WHERE ID IN :listAccIds]);
for (Contact con: Trigger.new){
Account myParentAcc = parentAccts.get(con.AccountId);
myParentAcc.Description = con.Description;
}
update parentAccts.values();
}