+ Start a Discussion

Contact Role, unit tests, and Opportunity AfterInsert trigger

I have a client who creates Opportunities from the Contact screen so that the Role is created.  When a new opportunity is entered, they want a field in the Contact record to be updated.  I've written an AfterInsert trigger on Opportunity to do this.  It references the role, of course.

However, I'm not sure how to write the unit test for this because of the role record.  How do I get the Opportunity insertion process to automatically create the OpportunityContactRole record?  If I have to insert the role record manually, then it's too late - the Opp has been inserted (to create the opp Id) and the trigger has fired already.


I've searched the boards but couldn't find anything relevant.





Any joy on this one?I want to do something similar.

I want to create an opportunity & attach the contact in 1 transaction. I have validation which dictates that the opportunity can only be created from the contact related list. So I need to accomodate that in my unit test.


Any one able to help?