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 

Populate the Contact Description with the ownerId and name whenever an opportunity is created.

Hi All,

I have written a code but the code is  not deploying as expected can somebody please help me rectify this code.
public class David_Update_desc {
    public static void updateContactMethod(List <Opportunity> OppList){
      Set <Id> IdCollect = new Set <Id>();
        For(Opportunity Opp : OppList){
            If(Opp.AccountId != null){
                IdCollect.add(Opp.AccountId);
            }
        }
        List<Contact> Con = [SELECT Id,
                                    Description
                             FROM Contact
                             WHERE AccountId IN :IdCollect];
        Map <Id, Id> OppMap = new Map<Id, Id>();
        For(Opportunity Opp : OppList){
            OppMap.put(Opp.AccountId , Opp.OwnerId);
        }
        
        If(Con.size() > 0){
            For(Contact c : Con){
                c.Description =  'the Creator is ' + OppMap.get(c.AccountId);
                 Update c;
            }
           
        }
    }}

Thanks.



 
Steven NsubugaSteven Nsubuga
Try this
public class David_Update_desc {
    public static void updateContactMethod(List <Opportunity> OppList){
        Set <Id> IdCollect = new Set <Id>();
        For(Opportunity Opp : OppList){
            If(Opp.AccountId != null){
                IdCollect.add(Opp.AccountId);
            }
        }
        List<Contact> Con = [SELECT Id,
                             AccountId,
                             Description
                             FROM Contact
                             WHERE AccountId IN :IdCollect];
        
        Map <Id, Id> OppMap = new Map<Id, Id>();
        For(Opportunity Opp : OppList){
            OppMap.put(Opp.AccountId , Opp.OwnerId);
        }
        
        Map<Id, User> OwnerMap = new Map<Id, User>([SELECT Id,
                                                    Name
                                                    FROM User
                                                    WHERE Id IN :OppMap.values()]);
        
        If(Con.size() > 0){
            For(Contact c : Con){
                c.Description =  'the Creator is ' + OppMap.get(c.AccountId) + '-' + OwnerMap.get(OppMap.get(c.AccountId));
            }
            Update con;
        }
    }
}

 
Maharajan CMaharajan C
Hi Akash,

Small Update on the steven code:

public class David_Update_desc {
    public static void updateContactMethod(List <Opportunity> OppList){
        Set <Id> IdCollect = new Set <Id>();
        For(Opportunity Opp : OppList){
            If(Opp.AccountId != null){
                IdCollect.add(Opp.AccountId);
            }
        }
        List<Contact> Con = [SELECT Id,
                             AccountId,
                             Description
                             FROM Contact
                             WHERE AccountId IN :IdCollect];
        
        Map <Id, Id> OppMap = new Map<Id, Id>();
        For(Opportunity Opp : OppList){
            OppMap.put(Opp.AccountId , Opp.OwnerId);
        }
        
        Map<Id, User> OwnerMap = new Map<Id, User>([SELECT Id,
                                                    Name
                                                    FROM User
                                                    WHERE Id IN :OppMap.values()]);
        
        If(Con.size() > 0){
            For(Contact c : Con){
                c.Description =  'the Creator is ' + OppMap.get(c.AccountId) + '-' + OwnerMap.get(OppMap.get(c.AccountId)).Name;
            }
            Update con;
        }
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
Steven NsubugaSteven Nsubuga
Thanks for catching that Raj, I had missed the reference to the Name field!!
Akash Choudhary 17Akash Choudhary 17
Thank You guys it worked...
Maharajan CMaharajan C
Hi Akash,

Please mark the best answer!!!

Thanks,
Raj