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
CushtyCushty 

update contact role field on closed won opportunity

Hi,

I am new to apex and an trying to write a trigger that will update a contact field after an opportunity is closed won.  These contacts are those contact roles attached to the opportunity.

I have a custom field on the contact and also the account object called Lifecycle Status and would like these to update to 'Customer' upon the opportunity becoming closed won.

First here is my bad code for trying to update the contact role on the opportunity to 'customer': 

trigger UpdateContact on Opportunity (after insert, after update) {

        OpportunityContactRole ocr;
        Contact contact;
        
        Opportunity opp = Trigger.new[0];
            if(opp.StageName == 'Won' && opp.Number_Of_Contact_Roles__c > 0)
            
          ocr = [Select o.Role, o.OpportunityId, o.IsPrimary, o.Id, o.ContactId 
         From OpportunityContactRole o where o.OpportunityId =: opp.id];

          contact = [Select c.OwnerId, c.Id, c.AccountId,c.name,c.Lead_Lifecycle_Stage__c 
          From Contact c where c.id =: ocr.ContactId ];
         
                {
                    contact.Lead_Lifecycle_Stage__c = 'Customer';
                    update contact;      
                }
}


I am getting a null error and asuming because its looking for the contact role upon saving the opportunity when its not there.  How do I list or map the associated contact roles to the opportunity and then upon one fire the update of the contact field and account field (although the account field maybe a separate trigger

Thanks for any help
Best Answer chosen by Cushty
Santosh Sriram 2310Santosh Sriram 2310
Hey Cushty

Hope this helps! If it works, please mark this as a solution.
trigger UpdateContact on Opportunity (after insert, after update) {

        OpportunityContactRole ocr;
        Contact contact;
        //You are not bulkifying the trigger --  am assuming it is okay!
        Opportunity opp = Trigger.new[0];
        //list of contact to update
        list<Contact> listToUpdate = new list<Contact>();
        //Checking for the condition
        if(opp.StageName == 'Won' && opp.Number_Of_Contact_Roles__c > 0){

            //Querying for all the contact roles related to the Opportunity
            for(OpportunityContactRole iterating_oppConRole : [SELECT o.Role, 
                                                                      o.OpportunityId, 
                                                                      o.IsPrimary, 
                                                                      o.Id, 
                                                                      o.ContactId,
                                                                      o.Contact.Lead_Lifecycle_Stage__c 
                                                              FROM OpportunityContactRole o where o.OpportunityId =: opp.id])
                                                              {
                //Getting the contact from the OpportunityContactRole
                Contact tempContact = new Contact(id=iterating_oppConRole.ContactId, Lead_Lifecycle_Stage__c = 'Customer');
                listToUpdate.add(tempContact);
               
           }
        }
        //Checking if we have some contact before we update
        //This will avoid attempt to dereference null object error
        if(!listToUpdate.isEmpty())
            update listToUpdate;
}

 

All Answers

Santosh Sriram 2310Santosh Sriram 2310
Hey Cushty

Hope this helps! If it works, please mark this as a solution.
trigger UpdateContact on Opportunity (after insert, after update) {

        OpportunityContactRole ocr;
        Contact contact;
        //You are not bulkifying the trigger --  am assuming it is okay!
        Opportunity opp = Trigger.new[0];
        //list of contact to update
        list<Contact> listToUpdate = new list<Contact>();
        //Checking for the condition
        if(opp.StageName == 'Won' && opp.Number_Of_Contact_Roles__c > 0){

            //Querying for all the contact roles related to the Opportunity
            for(OpportunityContactRole iterating_oppConRole : [SELECT o.Role, 
                                                                      o.OpportunityId, 
                                                                      o.IsPrimary, 
                                                                      o.Id, 
                                                                      o.ContactId,
                                                                      o.Contact.Lead_Lifecycle_Stage__c 
                                                              FROM OpportunityContactRole o where o.OpportunityId =: opp.id])
                                                              {
                //Getting the contact from the OpportunityContactRole
                Contact tempContact = new Contact(id=iterating_oppConRole.ContactId, Lead_Lifecycle_Stage__c = 'Customer');
                listToUpdate.add(tempContact);
               
           }
        }
        //Checking if we have some contact before we update
        //This will avoid attempt to dereference null object error
        if(!listToUpdate.isEmpty())
            update listToUpdate;
}

 
This was selected as the best answer
CushtyCushty
Hi,

This works fine! Thanks and i've learnt a lot.

How would I go about then extending this (or another trigger) to then update the corresponding Opportunities account to Customer equals true.
I will try a new trigger on opportunity and see how I get on?