Salesforce: Make an Opportunity clone for each Opportunity Contact Role along with Opportunity Products

Currently, the cloning of an opportunity does not clone the contact roles associated with the opportunity.

Usually, organization create a number of opportunities with identical or similar contact roles. We often create "template opportunities" which contain the common re-usable data which saves us a lot of time re-keying information for similar opportunities. The addition of the cloning of the contact roles associated with an opportunity would be tremendously helpful, as these contact roles are often forgotten in the cloning and update process of the new opportunity.


Here is the salesforce Idea: https://success.salesforce.com/ideaView?id=08730000000BqJYAA0.

I just came up with real quick solution as below:


Create a custom clone button:

Goto Setup --> Opportunities --> Buttons, Links, and Actions --> New Button Detail Button:

/{!Opportunity.Id}/e?clone=1&retURL=%2F{!Opportunity.Id}&opp5="Existing Business"& 
opp9={!TODAY()+30}& 
opp11=Placeholder 

cloneli=1 
&saveURL=/apex/CloneContactRoles?oldId={!Opportunity.Id}




  1. We are passing different parameters in url to change the value of corresponding fields for new Opportunity. Please add/remove field mapping by adding/removing them in parameter.
  2. We are redirecting to Visualforce page that would create contact roles under the new Opportunity

New Visualforce Page: CloneContactRoles


<apex:page standardController="Opportunity" showHeader="false" sidebar="false">
  <script src="/soap/ajax/38.0/connection.js" type="text/javascript"></script>
  <script>
  
    window.onload=function(){
        // Getting Session ID.
        sforce.connection.sessionId = "{!$Api.Session_ID}";
        var myquery = "SELECT ContactId, Id, IsPrimary, OpportunityId, Role FROM OpportunityContactRole WHERE OpportunityId = '{!$CurrentPage.parameters.oldId}'"; 
        var result = sforce.connection.query(myquery);
        var records = result.getArray("records");
        var croles = [];
        if (0 !== records.length) { 
            for (var i=0; i < records.length; i++) {
               var contactRole = new sforce.SObject("OpportunityContactRole"); 
               contactRole.ContactId = records[i].ContactId;
               contactRole.Role = records[i].Role;
               contactRole.OpportunityId = '{!$CurrentPage.parameters.newid}';
               contactRole.IsPrimary = records[i].IsPrimary;
               croles.push(contactRole);
            }
        }       

        //Create method 

        var result = sforce.connection.create(croles);
        window.location = '/{!$CurrentPage.parameters.newid}';            
    }
  </script> 
  
</apex:page>


Add this new visualforce page in your org and give it's access to corresponding profiles.


You can install these components directly to your org: 
https://login.salesforce.com/packaging/installPackage.apexp?p0=04t900000005GNT


Comments