Salesforce URL Hacking basics with retURL, saveURL and cancelURL

I have a rather large post about URL Hacking on Salesforce, but it occurs to me that some basics aren’t really addressed. Let’s address one of the fundamental components of URL hacking: navigation.

What happens to the URL when you edit a record in Salesforce using the standard edit button from the record detail page? It looks like this:

https://na10.salesforce.com/a0CA0000007RT6g/e?retURL=%2Fa0CA0000007RT6g

If we understand what every component of that URL is, we can “URL hack” it and modify the standard behavior so that we can customize navigation in our Salesforce application.

Let’s break the URL down from above:

  1. https://na10.salesforce.com/ — the domain
  2. a0CA0000007RT6g — the record we’re editing
  3. /e — the action for the record, e stands for edit in this case
  4. ? … — everything after the ? is the query string, which begins with a ? and is & delimited with key = value pairs, e.g. ?a=b&c=d&e=f, we have 3 variables being set: a which is set to b, c which is set to d and e which is set to f

Specifically above, the application is saying by default that we are editing the record a0CA0000007RT6g, and we want to the application to know that the retURL is set to %2Fa0CA0000007RT6g. %2F is url encoded (from a button or formula field we can use URLENCODE()), and once it’s decoded it becomes a forward slash, /. So once the URL is loaded, it knows that the retURL value is /a0CA0000007RT6g, which is the record we’re editing.

Let’s look at another example, open a Contact then click Edit next to a record on a related list. The URL that is loaded when I open the contact is

https://na10.salesforce.com/003F000001BPE2W

And the URL that is loaded when I click the edit link from the related list is

https://na10.salesforce.com/a0lF0000002Z86T/e?retURL=%2F003F000001BPE2W

a0lF0000002Z86T corresponds to the related record that I’m editing, and the retURL brings us back to the contact upon completion of editing.

What if we don’t want to go back to the Contact when we’re done, but instead stay on the order? It’s a simple URL hack to accomplish this non-standard behavior:

https://na10.salesforce.com/a0lF0000002Z86T/e?retURL=%2Fa0lF0000002Z86T

Now that we know how the URL is composed, we can predictably mold it to do what we want. retURL is a powerful URL parameter, and understanding how it works is fundamental to URL hacking.

There are other important URL parameters to keep in mind. What should we do if we want to redirect the user to a different place if they cancel the changes than if they save the changes? We can simply use saveURL and cancelURL for this purpose, or we can use them all three in conjunction.

The logic behind where the user is redirected on click of the Save button is as follows*:

  1. Is there a saveURL URL parameter set? If so, redirect to that.
  2. Is there a retURL URL parameter set? If so, redirect to that.
  3. Redirect to /home/home.jsp

*The behavior is slightly different if we’re creating a new record.

Similarly the navigation on the Cancel button follows:

  1. Is there a cancelURL URL parameter set? If so, redirect to that.
  2. Is there a retURL URL parameter set? If so, redirect to that.
  3. Redirect to /home/home.jsp

So if we want to specify a different location for the user to go on Cancel than on Save, we could simply supply a cancelURL and retURL. For example:

https://na10.salesforce.com/a0lF0000002Z86T/e?cancelURL=%2Fa0lF0000002Z86T&retURL=%2F003F000001BPE2W

This would bring us to the related record if we canceled, and bring us back to the Contact if we saved.

There are many other URL parameters used by Salesforce, but retURL, saveURL and cancelURL are the most fundamental.

In conclusion (tl;dr): we can use these fundamentals of url hacking for navigation to construct custom buttons and links to answer the question “can we forward the user somewhere else?” The answer: yes, absolutely.

This entry was tagged , . Bookmark the permalink.