You need to sign in to do that
Don't have an account?
Brian Hopkins 25
Replicate the automatic creation of OpportunityContactRole records, in an Apex Test Class?
When an Opportunity is created via a Contact, Salesforce will automatically create an OpportunityContactRole record.
When an after insert trigger is fired, the OpportunityContactRole records are already present.
However I cannot replicate this functionality in my Test Class. When I insert the opportunity, the trigger fires before I can insert the OpportunityContactRole records, so the test fails.
I need to either:
Triggers can not be added to the OpportunityContactRole object, or I'd have built it that way.
If you are not familiar with this process DO NOT RESPOND. (I'm sick of getting copy-pasted responses from people who didn't read the question.)
When an after insert trigger is fired, the OpportunityContactRole records are already present.
However I cannot replicate this functionality in my Test Class. When I insert the opportunity, the trigger fires before I can insert the OpportunityContactRole records, so the test fails.
I need to either:
- Reproduce that automatic functionality so that the OpportunityContactRole is inserted with the Opportunity
- Delay the firing of the trigger until after i have inserted the OpportunityContactRole
Triggers can not be added to the OpportunityContactRole object, or I'd have built it that way.
If you are not familiar with this process DO NOT RESPOND. (I'm sick of getting copy-pasted responses from people who didn't read the question.)
There is a workaround that you can try, its not a perfect solution but it might help.
- This block of code should be placed before line 11 where you are querying the OpportunityContactRole
- Now when you execute your test class theoritcally it should execute this block in the trigger and create an opportunitycontactrole and it should work.
I didnt got chance to try it out so it might take some tweaking to get it work. Hope this helps.All Answers
Since you have already written the trigger, can you share the trigger over here and then we can help you in writing test class for the same without copy pasting it from anywhere else :)
Hi Swati, thanks for your response.
And here is the Test Class I've written:Here's the function which is called by an after insert trigger on the Opportunity:
Thanks.
There is a workaround that you can try, its not a perfect solution but it might help.
- This block of code should be placed before line 11 where you are querying the OpportunityContactRole
- Now when you execute your test class theoritcally it should execute this block in the trigger and create an opportunitycontactrole and it should work.
I didnt got chance to try it out so it might take some tweaking to get it work. Hope this helps.The trigger itself will insert the extra OpportunityContactRole record, and then execute the rest normally.
Thanks for the suggestion, I don't know why I didn't think of that before!
Please mark this thread as solved if the solution works. This was a unique scenario and marking it solved might help others who stumble upon this in future.