Hi AdiTry the below code:
Hope this will be helpful.This worked for me perfectly.Thanks​​​​​​​trigger ContactTrigger on Contact (before insert,before update,after insert,after update) {
if(Trigger.isAfter)
{
if(Trigger.isInsert||Trigger.isUpdate)
{
//Update Account description with related contact description
Set<Id> accIds=new Set<Id>();
Map<Id,String> AccDesc=new Map<Id,String>();
for(Contact c:Trigger.new)
{
if(c.Description!=trigger.oldMap.get(c.Id).description)
{
accIds.add(c.AccountId);
if(AccDesc.containsKey(c.AccountId))
{
String descp=AccDesc.get(c.AccountId);
descp=descp+','+c.Description;
AccDesc.put(c.AccountId,descp);
}
else
{
AccDesc.put(c.AccountId,c.Description);
}
}
}
if(accIds.size()>0){
List<account> accList=new List<Account>([select id,Description from Account where id in: accIds]);
for(Account acc:accList)
{
if(AccDesc.containsKey(acc.Id))
{
if(acc.Description!=null)
acc.description=acc.Description+','+AccDesc.get(acc.Id);
else
acc.description=AccDesc.get(acc.Id);
}
}
update accList;
}
}
}
}
2 answers