You need to sign in to do that
Don't have an account?

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
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
Hope this helps! If it works, please mark this as a solution.
All Answers
Hope this helps! If it works, please mark this as a solution.
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?