Authored by Zishan Razzaq

Tuesday, September 11, 2012

How to copy Record Owner from Cloning Functionality in Salesforce

Hello everyone:

   the business wants to clone an Opportunity record but the Opportunity Owner of the cloned record should follow thru into the new Opportunity record.
Currently, when an user clones any record that user becomes the owner of the new record in Salesforce. URLFOR functionality does not work on this standard field Ownerid, I shall explain more see below for the solution. Also a workflow rule will not work as you would have to specify the Owner of the record.

This was tricky but its a solution with less code and utilizing more of the functions within salesforce.
Step 1:
  • On the Opportunity Object:
    • Create a new field:
      • OriginalOwner | OriginalOwner__c | Text(20) | Read Only | Visible to all 
      • The OriginalOwner field would take the cloned Opportunity Record Owner Id and place it in here when an user clicks on the clone button.
      • Notice in the URL the field ID, note it down because you will need that for the next Step. In my case the field ID is "00N50000002kItx".
      • Screenshot:
    • Create a customize Clone Button utilizing the "{!URLFOR" statement:
      • This would be a URL button with the following formula:
        • {!URLFOR( $Action.Opportunity.Clone,Opportunity.Id, [cloneli=1,opp11='Open'])}&00N50000002kItx={!Opportunity.OwnerId}
        • Breakdown of the formula: 
          • {!URLFOR($Action.Opportunity.Clone ==> The URLFOR leads the functionality of the action that would take place. $Action.Object.Functionality determines the Functionality that would happen. In this case, clone functionality for the Object "Opportunity"
          • cloneli=1 ==> This if you like the associated cloned Opportunity Products to follow thru to the new record. If you do not want to then cloneli=0. It will redirect them to select products after clone.
          • opp11='Open' ==> This determines the Stage. You can add more fields by utilizing opp1, 2, 3 and so fourth. Please utilize this website to get the list of standard fields for this formula. How to obtain a field id 
          •  &00N50000002kItx={!Opportunity.OwnerId} ==> This is the new custom field we created and the {!Opportunity.Ownerid} is the field we need to copy over from existing record.
        • Screenshot:
      •  Now to write a small trigger to do the work after the clone. The button will get you the ownerid for the existing record but it will not update the ownerid. The trigger will.
        trigger owneridchange on Opportunity (before insert) {
        Opportunity[] a =;
            if ([0].OriginalOwner__c!=null || 
         This trigger will only kick off on clone functionality only and 
        not when a new opportunity record is created.
        • Now place the customize clone button on the page layout and go to a record that is not yours and clone the opportunity you will notice that the id gets filled into the new field OriginalOwner and then the trigger will fire off and change the Owner by placing in the id from the OriginalOwner field.
        Hope this helps.
        Watch Me Fight 
        Join My Facebook on Cage Assassin Fight Gear  Like my fan page Cage Assassin Fight Gear  

1 comment:

  1. Hi Jishan, Do you happen to have the test class for this trigger handy?