Clone Salesforce Records Using a Custom Button Or Link
Almost four years ago I wrote the first post for this blog. That post had to do with passing field values in a URL in order to populate those values on an edit page. What's interesting is that the original post still gets a ton of traffic. So I'd like to expand on that topic again in this post and illustrate how you can build on that original logic in order to do something useful. In this case, I want to create a custom button that will clone a User record and remove certain fields so that I don't have to fill out so much information when I create new Users.
Anyone who is an admin for any larger enterprise knows that creating Users can get tedious and over time there are a number of custom fields that you probably have on your User records. The logic I'll demonstrate here will allow you to save a bit of time the next time you need to create a new User. Or you could just take the information from this post to create a "Clone" button on another object - like Account. Because the Account object doesn't have a standard "Clone" button and that may be useful to you as well.
For the purposes of this post I am going to presume that you are already familiar with the requirements for passing parameters in a URL. If you are not familiar with this then I recommend that you take a few minutes to read my original post because that information is contained within it. I am also going to presume that you are familiar with creating custom buttons/links and adding them to any page layout. This isn't covered in the post I reference above but you can learn about this in the standard Salesforce help docs.
The first thing we need to know in order to make this work is the base logic that Salesforce uses for clone functionality. Trying to stay out of the weeds, Salesforce basically needs to know the record Id that you want to copy (or clone). The platform also needs to know that the request is, in fact, a clone request. The anatomy of that logic is as follows:
https://na1.salesforce.com/00500000000xXxx/e?clone=1
What you can see in the line above is that the record Id being cloned is "00500000000xXxx" and the clone indicator is the "clone=1" portion of the string. If you want to see this in action simply go to any Contact record and click the "Clone" button. You'll see that the request essentially looks identical to that above. Of course, there will be subtle differences in the salesforce.com domain, the actual record Id will be different and there will likely be some additional parameters being passed within the URL. But you get the idea.
Now that you've got the basics for how Salesforce knows to clone a record from the URL request, you may be thinking that you should pass some additional information in the URL to make life easier. Again, the context of this post is to make a custom button for clonoinig an existing User record. You could simply clone the full User record and then remove the attributes that you want to change but you may mistakenly leave something that needs to be altered for all Users that get created. Specifically, things like the first name, last name, email address and anything else that may be useful when you create a new user.
Here's a list of the things I think we need to include in the URL:
So you might be thinking... WTF is all that crap bulleted above? At least that's my initial reaction when I read it out loud. Basically, I am trying to illustrate the things that I think we can append to our button logic in order to make life easier when we eventually get to clicking the "Clone" button.
Let's take a step back and look at the very basic principle I am trying to illustrate here. Get to the screen where you can add a custom link for the User object (Setup > Customize > Users > Custom Links > New). On that screen you will add the following information:
The last piece needed is the string to use as the URL. If you look back at the top of this post you see that all we really need in order to clone a record is the record Id and the "clone=1" parameter. Since we're all using Salesforce here we can keep the URL relative. Meaning we can drop the whole "na1.salesforce.com" crap at the beginning of the URL. This will actually simplify how your link looks anyway. Also, we want this logic to work from any User record so there is no need to hard code a specific User Id in the URL either. We can simply use the merge field for the User record and Salesforce will take on the task of providing the proper Id when we click the link. The merge field for User Id is "{!User.Id}". So the last portion of our custom link will look like this:
Make sure you don't actually put the quotes in the field on the page. I am just using them here to illustrate the exact test to use in the URL string. Also be sure to include that initial "/" character. It's important and allows Salesforce to properly recognize the link.
At this point, If you save the custom link and add it to your page layout you will find that it works. You can clone a User record. But you'll also notice that everything gets cloned. Which means that you have to change at a minimum the name, email address and username of any User you create from this new button. So this brings me back to that crap I wrote about earlier that I recommended we include in the URL. By passing the field name in the URL but leaving nothing after the equal sign we will force the platform to remove the values from the cloned record. Again here are the things I recommend get included in the URL request:
Field | URL Safe Name | Value |
---|---|---|
Last Name | name_firstName | null |
Last Name | name_lastName | null |
Alias | Alias | null |
null | ||
Username | Username | null |
Community Nickname | CommunityNickname | null |
Cancel button functionality | retURL | Cloned User record |
Appending all of these values to our original URL results in the following:
/{!User.Id}/e?clone=1&retURL=%2F{!User.Id}&name_firstName=&name_lastName=&Alias=&Email=&Username=&CommunityNickname=
Update your custom link to this string and try it out. I think you'll dig how much time it saves. Or you'll dig that it actually does what I said it would and will lead to you building other great custom buttons/links on other objects in your Salesforce org.
One final point about passing parameters in the Salesforce URL. Anyone familiar with salesforce.com will understand that the logic described in this post is basically a hijacking of standard Salesforce functionality. As such Salesforce may change how the platform logic works and this post could become obsolete. Blah, blah, blah... If you read from the beginning you'll also recall that I first wrote about passing variables in the URL in 2007. And this is something I've been doing since 2003. Salesforce has changed over that time frame but this URL parameter logic has essentially remained unchanged for as long as I can remember. Nonetheless, use this information at your own risk.