Just a quick warning before we get started: this week’s post is going to get a little technical, but if you take a deep breath, cuddle up with your coffee mug (I prefer tea myself), and have Salesforce on the ready to try things out, you’ll be just fine I promise.
As your org grows and new fields are added, the time it takes users to enter new records creeps ever-closer to the point of “are you kidding me”. You may want values like the Account automatically populated when a user creates an Opportunity from a Contact, but you’re going to have to bring in a developer who will spend hours on coding and testing, and you may still have to deal with future maintenance. The more fields, the more development hours. Your users are complaining about the effort to just drop a new record in the system, but the cost to make it easier just doesn’t make sense. Well, with a few custom buttons, you can chop away at that effort today, no developer required.
Ok, now, take a sip of coffee and open up Salesforce in a couple tabs before continuing. Ready now?
Go to the object you want to add the button under. For this example, we’re going to create a Case from the Contact record. Now, click through to any record (Contact here), go to the related list in question, click New, and copy the URL. It should look something like this:
Now, in another tab, go to the target object (Case here) in the backend, go to Buttons and Links, and click “New Button or Link”. Set the name to “New” (or whatever you’d like), select “List Button”, under Behavior select “Display in existing window without sidebar or header”, and set Content Source to “URL”. Paste your URL into the big text box and strip away the first segment so that it looks something like this:
(Note: The “/500/” tells the system which object you’re working with. The “e” before the “?” stands for Edit, because you’ll be on an edit page.)
Now the real fun begins. Let’s first make this button dynamic by replacing the IDs we got from the original URL with merge code. For this, under Select Field Type, change it to Contact, then choose Contact ID. Replace the Contact IDs (big long string of numbers and letters here starting 003) with the thing the system spat out. Do the same for the Account (the piece starting 001):
(Note: The “retURL” stands for Return URL, which is where you’ll be redirected if you click Cancel. The “%2F” stands for “/”. Because adding a “/” in the URL would confuse your web browser, this code is needed. “def_contact_id” and “def_account_id” are the IDs for the Contact and Account which will be automatically populated.)
After that, we need to start dropping in the other fields you want to fill in. In my org, many of my Contacts have assistants who I need to be in touch with, so I have a lookup to Assistant (Contact) on both my Contact and Case records. To get that information in, I’ll open another tab, go to the Case fields in the back end, drill down to the Assistant field, and take the ID from the URL. Here’s the URL:
And here’s the ID: 00NE0000005IhLH
Go back to your button code, add an & to the end followed immediately by “CF” then the ID you just found and then an =. Your new code will look like this:
(Note: The “CF” you place at the beginning of the field ID stands for Custom Field. This is how the system identifies that this is a field which needs to take a value.)
Again, use the Select Field Type and Insert Field dropdowns to select the Assistant Name and add that directly after the = sign. Because this is a lookup, we also have to set the ID. Copy the piece before the merge code (from & to =), then before the =, add “_lkid”. Now insert the Assistant ID. Your code should look something like this now:
(Note: You will only need to add “_lkid” for Lookup fields. You can skip this step if you want to add something to a text or number field.)
Use Quick Save to save your button (this way you don’t have to click back in to edit it). Now go back to the object you want to start from (Contacts), click “Edit Page Layout”, scroll all the way down to the bottom and click edit next to the target object (Cases) related list section. At the bottom, you’ll see a Buttons section. Expand that, deselect the New button checkbox, then add your new button under Custom Buttons to the right hand side and click Ok then Save. When you get back to your record, click the New button, and if everything is right, it should work like a dream!
Because there are thousands of things you can do with this functionality, this post could go on for hours. For the sake of not giving you a migraine, I’ll let you play around with this for now. If you need any help, I’d recommend starting with Google or popping over to #askforce to get quality support in 120 characters or less. And don’t forget, you can always drop us a line.
-Jared and the Salesforce Guys