Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
2 answers
  1. Oct 29, 2018, 10:38 AM
    Hi Adi

    Try the below code:

     

    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;

    }

    }

    }

    }

    Hope this will be helpful.This worked for me perfectly.

    Thanks​​​​​​​
Loading
0/9000