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
Brian Hopkins 25Brian Hopkins 25 

How to include URL parameters as part of a Trigger Test class?

Greetings,

So.  I've replaced the standard OpportunityContactRoles object with a custom one, Contact_Roles__c
It does basically the same thing, acts as a junction object between Contacts and Opportunities.

When a new Opportunity is created from a Contact page, the Opportunity edit page has the parameter conid=XXXXXXXXXX.
When saving the Opportunity, Salesforce uses this conid parameter to generate an OpportunityContactRole.

I've built a after insert trigger on the Opportunity, that queries OpportunityContactRole and duplicates them as Contact_Roles__c records.
That part works perfectly.

Now I am trying to write a test class for this, but I can't figure out how to pass in the conid parameter to the Opportunity insert function.
Keep in mind that this is a Trigger, not a Controller

How, I ask, would I accomplish this?  Do I still need to mess around with page controllers to emulate this functionality?
Prateek Singh SengarPrateek Singh Sengar
Hi Brian,
Triggers dont work on page parameters, Since your trigger is on opportunity insert you just need to create a test opportunity data and insert in the test class, this should execute the trigger and you should get your coverage.
Just ensure that the opportunity record that you are creating as part of the test class is satisfying the condition in your trigger.
 
Brian Hopkins 25Brian Hopkins 25
Prateek, thanks for your response but I don't think you understand the problem.

My Trigger depends on there being OpportunityContactRole automatically created when an Opportunity is created.  
This built-in function of salesforce depends on the conid parameter in the URL.

I need to replicate this built-in functionality in the test class.
Or, I need a way to delay the firing of the opportunity trigger until after I insert an OpportunityContactRole, so that it already exists when the Opportunity trigger is fired.
Brian Hopkins 25Brian Hopkins 25
Still having this issue.  Does anyone understand how Opportunities work in Salesforce?