How to create a button that auto-fills in field on a new record
This article explains how to create a custom button on a related list so when clicked to create a new record on a different object, it will auto-fill in information from the record where the original related list was located.
This is a bit of an advanced technique, often called "URL Hacking." It's not officially supported by Salesforce.
For more information:
In this simple demo, we're going to create a custom button on a contact's Opportunity related list to create a new opportunity that automatically sets the Opportunity Stage to Posted, sets the Close Date to today's date and puts in "Set by Workflow Rule" as the Opportunity Name (so a workflow rule can update when the record is saved).
You can follow these steps to set the record type and fill in other fields as well. This is a basic example.
First, create the button
- Give the button a distinctive Label, tab to complete Name
- Fill in a description of the button's expected behavior
- Select List Button and uncheck Display Checkboxes
- Select Display in existing window without sidebar or header
Click Quick Save so you can save your work so far without closing this window. Leave this window alone and open a new tab in your browser that is also logged in to Salesforce. You'll get back to this page in a moment.
Get the starting URL
In this case, we're not actually creating a record. What we want is the URL. Copy the entire thing to your clipboard.
First, clean up the URL.
You don't need the domain (https://naxx.salesforce.com) so take it out. Your URL should start at the first / after salesforce.com
Put hard returns before each "&" so you can clearly see how the URL is structured. Be careful to have absolutely no spaces in the URL. Returns are fine. Spaces are not.
Note: lookupcmpgn=1 will automatically fill in the active campaign. Most folks would rather select their own campaign so feel free to remove this line completely.
Now you can start replacing the info that is specific to this contact with the necessary merge fields.
Since you're running this from the contact record, you need to make sure to select contact fields only to merge in. For example, you can't merge in Household fields unless they're on the contact record itself.
For text values, you can replace with actual words as shown. Remember, no spaces!
Here's the code in a single line:
Note: The record type ID is unique for each organization. You must replace with your own organization's equivalent record type ID or this code will not work.
Add button to page layout
It worked! The Opportunity Name is now auto-filling in with what you selected.
Now we have to add in that we want it to set the close date to today and the Stage to Posted.
We need to get the value to put in as &(something)=Posted or &(something)=Today.
Here's how to get the (something).
This is in Chrome but you can do this in any browser that shows the HTML specific to a select area of the page.
In Chrome, right click on the field you want to use and select Inspect element from the menu.
Automatically, the bottom half of the screen will show the code behind it, with the right line highlighted. Look for the words: name="(something)" and copy the (something) to your clipboard.
You can take advantage of formulas in your URL. In this case, you can easily insert Today's date as shown.
Save the button.
Test your final button