KELL Solutions and KnowledgebaseGeneral Salesforce Resources Helpful Configuration Tips & TricksHow to create a button that auto-fills in field on a new record

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:

Our Example

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

Even though you'll be clicking the button on a contact record, the button itself lives on the object of the record that you are creating. So in this case, it's an opportunity button. Navigate in Setup to Customize > Opportunities > Buttons, Links, and Actions.

First, create the button

Click New Button or Link.

  1. Give the button a distinctive Label, tab to complete Name
  2. Fill in a description of the button's expected behavior
  3. Select List Button and uncheck Display Checkboxes
  4. 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

Go to a contact record and click an existing button. This is the button you are going to replace. In the Nonprofit Starter Pack, if you don't want to change the record type it's easiest to replace the New Contact Donation button as shown.

Get the starting URL

Select the record type if necessary. For this example, we're not going to pre-select the record type but it's possible to create a button that does so. This is more complicated, so for now we'll leave it as is. Click Continue.

In this case, we're not actually creating a record. What we want is the URL. Copy the entire thing to your clipboard.

Go back to the button you started working on and paste in the URL.

This is just our starting point. It's unique to the contact record you clicked from. Now we have to edit this to make it generic for any contact record.

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:

/006/e?retURL=%2F{!Contact.Id}&accid={!Account.Id}&conid={!Contact.Id}&RecordType=replace&ent=Opportunity&opp3=Set+By+Workflow+Rule

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.

When you click Save (or Quick Save), you're reminded that you must add this button to a page layout to use it.

Add button to page layout

On the contact layout, scroll to the Opportunities related list and click the wrench icon.

Add button to page layout

Click on the gray Buttons bar

Add your new button to the Selected Buttons side.

Click Ok and save the page layout.

Test your button so far

Go to a contact record and click on your new button.

Test your button so far

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.

Go back to your button code and paste in the result. In this case, we want the value to be "Posted" as shown.

Click Quick Save so you don't lose your progress.

Repeat the same steps, but this time highlight the Close Date field. Now you can see that Name is "opp9" Copy opp9 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