login

Tip of the Week

Tip of the Week – Hack away at work with URL hacking in Salesforce

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:

https://na9.salesforce.com/500/e?retURL=%2F003E000000ppK6E&def_contact_id=003E000000ppK6E&def_account_id=001E000000oA7Ym

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:

/500/e?retURL=%2F003E000000ppK6D&def_contact_id=003E000000ppK6D&def_account_id=001E000000oA7Yn

(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):

/500/e?retURL=%2F{!Contact.Id}&def_contact_id={!Contact.Id}&def_account_id={!Account.Id}

(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:

https://na9.salesforce.com/00NE0000005IhLH?setupid=CaseFields

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:

/500/e?retURL=%2F{!Contact.Id}&def_contact_id={!Contact.Id}&def_account_id={!Account.Id}&CF00NE0000005IhLH=

(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:

/500/e?retURL=%2F{!Contact.Id}&def_contact_id={!Contact.Id}&def_account_id={!Account.Id}&CF00NE0000005IhLH={!Contact.Assistant__c}&CF00NE0000005IhLH_lkid={!Contact.AssistantId__c}

(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

, , , , ,

9 Responses to Tip of the Week – Hack away at work with URL hacking in Salesforce

  1. Johan January 27, 2014 at 3:41 am #

    Nice one Jared!

  2. David Berman January 27, 2014 at 11:15 pm #

    Hi – thanks for the post! One thing I’ve been struggling with is the Owner field on a custom object. It’s the standard Owner field, when you select ‘Change Owner’ you select between Users and Queues.
    I’ve got queues set up and have been trying to pass the Owner field and the queue ID via the URL, but it’s not working.
    There’s no error.. just nothing happens. Do you know how to pass a set queue ID to a custom object’s Owner field?

    • Jared Baker January 27, 2016 at 5:20 pm #

      @David,

      Sorry for the (VERY) delayed response, your comment got stuck in a spam filter – don’t worry, I’ve fixed that.

      I’ve done some research here, and while you can create a URL hack to dynamically populate the new owner on the “Change Owner” page (newOwn_lkid={User ID}&newOwn={User Name}), it appears you can’t pass a value into the Owner field directly on the edit page. I would instead recommend using a Process or Workflow to populate based on some criteria after the record saves.

  3. Jeff February 9, 2015 at 3:59 pm #

    Great Article! It was very helpful to create some custom buttons that automatically pulled some information when creating new records. I would love to see a follow up with some more advanced features such as using functions and conditional statements inside these URLs. I figured out some basics messing around but it would be great to get some ideas of syntax for for advanced functions.

    • Jared Baker February 12, 2015 at 11:55 am #

      Hi @Jeff,

      That’s a great idea! Do you have any thoughts about the kinds of advanced functions you’d like to see? I’m sure I could make something up, but if you’ve got some suggestions, we always like to make our posts relevant to our customers and readers. 🙂

  4. Juliana M. April 1, 2015 at 1:11 pm #

    Love it! I was wondering if you knew how to pass a parameter to filter/search a Report Folder? I can create an URL to go to a specific Report Folder, but what I really want to do is create a URL that will go to the Report Folder with a dynamic search parameter embedded.

    Here are the specific details:

    I have created a report/dashboard folder with a series of complicated dashboards. Each dashboard, however, is intended for a particular user. (Anywhere from one to five dashboards per user.) I have a Home Page custom link that takes the user to the dashboard folder in question, but what I really want to do is automatically filter the list of dashboards based on the user name. Then if I put the appropriate user name in the dashboard description, a search will only show their dashboards.

    I don’t care if the users can see each other’s dashboards, I just want a way to give them a list of their dashboards when they click on the home page link.

    Do you know if you can pass a search parameter in a URL and how to do it?

    Thanks!!!

    • Johan January 27, 2016 at 5:40 pm #

      I’m thinking this would be visualforce + apex code to redirect a link based on user detail.
      Not sure if a javascript link can do it.

Trackbacks/Pingbacks

  1. Troubleshooting Workflow Rules in Salesforce is easy - July 24, 2014

    […] been a while since I’ve advised you to brew a cup of coffee before reading one of our posts, but that might not be a bad idea this week. Don’t panic though. […]

  2. It’s easy to create dynamic Reports in Salesforce - January 30, 2015

    […] time in your day. You’ll probably want to grab a cup of coffee like you did when you read our URL hacking guide, but know that this is only skimming the surface of what you can do with dynamic […]

Leave a Reply to Juliana M. Click here to cancel reply.