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
Akash Choudhary 17Akash Choudhary 17 

Want to update contact's description if opportunity Description is updated

Hi all ,

This is my code , I want to update Contact's description everytime I update opportunity's description.
This is not working somehow . please help me figure out my mistake

public class DESC_UpdateForum {
    public static void DESC_UpdateForumMethod (list<Opportunity> Opplist){
        
        Set<Id> idSet = new set<Id>();
        For(Opportunity Opp : Opplist){
            if(Opp.AccountId != null){
                idset.add(Opp.AccountId);
            }
        }
        List <Contact> con = [SELECT Id,
                                     AccountId,
                                     Description
                              From Contact
                              Where AccountId IN :idSet];
        Map<Id , String> Oppmap =  new Map<Id, String>();
            For(Opportunity Opp : Opplist){
                If(Opp.Account != null){
                    Oppmap.put(Opp.AccountId , Opp.Description);
                }
            }
        if(con.size()>0){
            For(Contact c :con){
                If(c.AccountId != null){
             c.Description = Oppmap.get(c.AccountId);
                }}
        }
        Update con;
    }}

Thanks
Shruti SShruti S
Sorry for the dumb question, but are you executing this function from an Opportunity After Update Trigger?
Akash Choudhary 17Akash Choudhary 17
Hi Shruti, 
Yes this is after update and this was not a dumb question .
Akshay_DhimanAkshay_Dhiman
Hi Akash,

Please try the below code:
 
public class DESC_UpdateForum {
    public static void DESC_UpdateForumMethod (list<Opportunity> Opplist){
        Set<Id> idSet = new set<Id>();
        For(Opportunity Opp : Opplist){
                idset.add(Opp.id);
        }
        List<OpportunityContactRole> conList = new    List<OpportunityContactRole>();
        conList =[SELECT Id,OpportunityId,ContactId FROM OpportunityContactRole WHERE OpportunityId in :idSet];
        system.debug('conliat------>'+conList);
        set<id> Idcon  = new    set<id>();
        for(OpportunityContactRole co :  conList){
            Idcon.add(co.ContactId);
        }
        List<Contact> coList = new List<Contact>();
        coList = [SELECT id,Description from Contact where Id in :Idcon];
         List<Contact> coList1 = new List<Contact>();
        for(Opportunity op : Opplist){
            for(Contact con : coList){
                con.Description = op.Description;
             coList1.add(con);
            }
        } 
        update coList1;
    }
}
//Trigger Class
trigger UdateContactDiscription on Opportunity (before update) {
    if(trigger.isUpdate){
        DESC_UpdateForum.DESC_UpdateForumMethod(trigger.new);
    }

}

 If you found this answer helpful then please mark it as best answer so it can help others.   
   
  Thanks 
  Akshay