How To: Load a Salesforce or Visualforce page after saving on the Clone, Copy or Edit Super Clone pages
March 4, 2017
How To: Use conditional logic to control the logic of the Custom Button
March 31, 2017

How To: Make an Opportunity clone for each Opportunity Contact Role

This example code will clone the opportunity for each opportunity contact role. It uses the ‘selectwhere’  url parameter to filter the contact roles, and each new opportunity will have a single contact role record.

A Javascript custom button is used with Salesforce Ajax toolkit. The Ajax toolkit queries the OpportunityContactRole object by the opportunity record id. Javascript logic loops through the results to build the url, and then it loads the page for the user. The user is presented with a clone confirmation page that will display the opportunity to clone with each individual contact role record.

The ‘selectwhere’ takes 4 parameters that are delimited by ‘:’. Multiple where conditions can be included by delimiting them by ‘;;’. Including an ‘OR’ condition can be done by adding ‘||’ before the object name in the first parameter. If the object reference or field name is invalid, the logic will ignore the filtering logic, and all records will be included when cloning.

  • selectwhere=OpportunityContactRole:ContactId:=:003j000001UM91gAAD
    • Parameter 1: The object reference. This may be the API name of the object or the CloneObject__c record id. The record id is can be used if the same object is included at multiple levels of the clone hierarchy.
    • Parameter 2: The API field name that will be filtered on.
    • Parameter 3: The comparison operation. Valid values include =, !=, <>, >, and <.
    • Parameter 4: The value that is the field is being compared to.

In the example below, the configuration name is ‘MyOppCfg’. Your configuration name can be something else, but you would need to update the script to reflect the different name.

The URL will look similar to this:

https://lcrm-scp.na16.visual.force.com/apex/scpClone?rid=006j000000Yu2F9&cfg=MyOppCfg&saveURL2=%2Fapex%2Flcrm-scp__scpClone%3F%26rid%3D006j000000Yu2F9%26cfg%3DMyOppCfg%26selectwhere%3DOpportunityContactRole%3AContactId%3A%3D%3A003j000001UM91gAAD&retURL2=%2Fapex%2Flcrm-scp__scpClone%3F%26rid%3D006j000000Yu2F9%26cfg%3DMyOppCfg%26selectwhere%3DOpportunityContactRole%3AContactId%3A%3D%3A003j000001UM91gAAD&selectwhere=OpportunityContactRole%3AContactId%3A%3D%3A003j000001UM91hAAD

The user would click the custom button on the Opportunity page.

 

  • They would be redirected to the first clone confirmation page.

  • Then they would be redirected to the second clone confirmation page.

  • Finally, the use would be presented with the last opportunity that was cloned after all cloning was complete.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.