function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jishan royjishan roy 

Opportunity description field display on account description field

hii everyone,

I write trigger for opportunity description field display on account description field, but my trigger working for only latest opportunity description i want all account related opportunity description showed in account description.

this is my code:
trigger UpdateAccountDescription on Opportunity (before insert, before update, after insert, after update) {
    List<Opportunity > oppList =new List<Opportunity >();
    Set<Id> setid = new  Set<Id>();
    
    if(trigger.isBefore){
        system.debug('trigger before event');
        oppList = trigger.new;
        
    }else if(trigger.isAfter){
        oppList=trigger.new;
        for(Opportunity opp:oppList){
            setid.add(opp.AccountId); 
        }
        
        system.debug('setid ' + setid);
        
        List<Account> accList = [Select Id, Name, Description From Account  Where Id=:setid];
        
        if(trigger.isInsert){                     
            
            for(Opportunity opp1:trigger.new){
                for(Account a1:accList){
                    a1.Description= opp1.Description+ ' ';
                    update a1;
                }   
            }
            
        }else if(trigger.isUpdate){
            for(Opportunity opp2:trigger.new){
                for(Account a2:accList){
                    a2.Description= opp2.Description+ ' ' ;
                    update a2;
                }   
            }
            }
        }
    }




Thanks in advance!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jishan,

Can you try code as below.
 
trigger UpdatedescriptiononAccount on Opportunity (after insert,after update) {
    String description;
    set<Id> accountids= new set<Id>();
    List<Account> accottoupdate= new List<Account>();
           For(Opportunity opp: Trigger.new){
            
           if(trigger.isinsert || (Trigger.isupdate  && opp.Description !=Trigger.oldmap.get(opp.id).description))
    {
  accountids.add(opp.AccountId);
        }
    }
    
   List<Account> mapaccount= new List<Account>([select id,Name,Description from Account where id in :accountids ]);
    
    For(Account acc:mapaccount ){
        
        List<Opportunity> opplist=[select id,Description from Opportunity where accountid =:acc.id];
        For(Opportunity opp:opplist){
            if(opp.Description!=''){
           description= description+ opp.Description;
            }
        }
        acc.Description=description;
        accottoupdate.add(acc);
    }
    update accottoupdate;

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
jishan royjishan roy
hii sai praveen,

your code is working good only when any opportunity deleted then account description field stay as it is not deleted that description.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jishan,

Do you want to handle in delete condition as well?

Thanks,
 
jishan royjishan roy
hii sai praveen

yes i think sooooo!
Shri RajShri Raj
trigger UpdateAccountDescription on Opportunity (before insert, before update, after insert, after update) {
    Set<Id> accountIds = new Set<Id>();
    
    if (trigger.isAfter) {
        for (Opportunity opp : trigger.new) {
            accountIds.add(opp.AccountId);
        }
        
        List<Account> accounts = [SELECT Id, Description FROM Account WHERE Id IN :accountIds];
        Map<Id, Account> accountMap = new Map<Id, Account>(accounts);
        
        List<Opportunity> relatedOpps = [SELECT Id, Description, AccountId FROM Opportunity WHERE AccountId IN :accountIds];
        
        for (Opportunity opp : relatedOpps) {
            Account acc = accountMap.get(opp.AccountId);
            acc.Description = acc.Description + ' ' + opp.Description;
        }
        
        update accounts;
    }
}

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jishan,

Can you try below.
 
trigger UpdatedescriptiononAccount on Opportunity (after insert,after update,after delete) {
    String description;
    set<Id> accountids= new set<Id>();
    List<Account> accottoupdate= new List<Account>();
    if(Trigger.isinsert || Trigger.isupdate){
           For(Opportunity opp: Trigger.new){
            
           if(trigger.isinsert || (Trigger.isupdate  && opp.Description !=Trigger.oldmap.get(opp.id).description))
    {
  accountids.add(opp.AccountId);
        }
    }
    
   List<Account> mapaccount= new List<Account>([select id,Name,Description from Account where id in :accountids ]);
    
    For(Account acc:mapaccount ){
        
        List<Opportunity> opplist=[select id,Description from Opportunity where accountid =:acc.id];
        For(Opportunity opp:opplist){
            if(opp.Description!=''){
           description= description+ opp.Description;
            }
        }
        acc.Description=description;
        accottoupdate.add(acc);
    }
    update accottoupdate;
    }
    
    if(Trigger.isdelete){
       For(Opportunity opp: Trigger.old){
            
          
  accountids.add(opp.AccountId);
        
    }
    
   List<Account> mapaccount= new List<Account>([select id,Name,Description from Account where id in :accountids ]);
    
    For(Account acc:mapaccount ){
        
        List<Opportunity> opplist=[select id,Description from Opportunity where accountid =:acc.id];
        For(Opportunity opp:opplist){
            if(opp.Description!=''){
           description= description+ opp.Description;
            }
        }
        acc.Description=description;
        accottoupdate.add(acc);
    }
    update accottoupdate;
    }  
    }

If this solution helps, Please mark it as best answer.

Thanks,