In Blog, Releases

URL Hacks are back and in Lightning with Spring ’20

Before the coming of Salesforce Lightning Experience, I was a big fan of URL Hacks. These not officially support features gave us to the capability to change Salesforce funtionalty by simply changing the information in the URL. Common use cases was to pre-fill the “new record” screen or fill in or change a field in the “edit record” screen. We could do some crazy automation-like things. I wrote about URL Hacks a lot.

Then Lightning Experience cam and with it, the official outlawing of URL Hacks.  Instead, we were given actions and ability to pre-fill forms that were not the full “new” or “edit” screens but some poor approximation. We also lost all the other cool things we used to be able to do by simply hacking the URL. I grew despondant. I was a very vocal advocate for doing something. I was so sure that URL Hack functionality needed to be there, that I figured out how to do it with Flow. Combining Flow and Lightning Component Local actions we could do a lot that was URL Hack like. Doug Ayers took my concept one step further and gave you an actual URL hack -albiet one that required a lot of code.

I was very surprised when I opened up my copy of the Salesforce Spring Release Notes and discovered that URL Hacks are back in Lightning! Kind of, some what… and I also discovered that despite years of screaming I want URL Hacks, I’m pretty annoyed at this.

So URL Hacks are back in town. Let’s look at what you can do with them and why you may not want to use them. On the bright side, I get to re-use a feature image from a previous post.

How They Work

URL Hacks in Lightning are a bit better than in Classic in one way – the URL is easier.

In classic, we had to find out what the Field’s Name/ID was in the HTML and reference that in the URL. The big problem was those references changed from Sandbox to Production and potentially whenever your instance changed. It was very annoying since you couldn’t pre-fix things. You had to wait until they were broken and THEN fix the issue.

The example the Release Notes provide looks like this:

/lightning/o/Account/new?defaultFieldValues=
    Name={!URLENCODE(Account.Name)},
    OwnerId={!Account.OwnerId},
    AccountNumber={!Account.AccountNumber},
    NumberOfEmployees=35000,
    CustomCheckbox__c={!IF(Account.SomeCheckbox__c, true, false)}

Let’s break that down.

When we create a new Account the URL is currently: /lightning/o/Account/new?

That’s identical to the URL hack and hasn’t changed. The new bit comes after the question mark and that’s “defaultFieldValues=” In our URL Hack Parlance this is a URL Parameter. After the questionmark the browser knows anything afterwards is a parameter. All parameters are in a key=value format. So for this example, our key is defaultFieldValues and our values are a combination of Keys and values for the field and their values.

Name={!URLENCODE(Account.Name)} this part is important. The important bit is URLENCODE. This is a function that will take any non-url friendly character and convert it to a friendly version. For example, if the account name was “Better Partners” URLENCODE will turn it to “Better%20Partners” Spaces are not acceptable in the URL Format. Anything that is not just a list of numbers, a single word, or contains any funky characters will need the URLENCODE function

Then we have a comma to separate the next field and value pair. This is significant since in typical URL Hack we would separate fields with a & to denote a new parameter. In this circumstance all the Field/Value pairs are really just 1 value to the defaultFieldValue key. So it’s not as simple as copy/pasting existing URL hacks from classic. You will need to do some conversion.

Why I No Longer Like URL Hacks

First, I totally understand why people like URL Hacks. They’re fast. They’re “easy.” They gave us access to the full page layout. We could also do some funky other stuff in Classic.

In Lightning, we can only create new records. While this is nice, I personally prefer to use a Lightning Component or a Flow with a Lightning Component action instead to get the URL Hack functionality in Lightning.

URL Hack Functionality In Lightning?!

Why? Well for a few reasons. First, I think it gives us more ability in the long run. Yes, you need to have a custom lightning component, but in the circumstance for creating new records it’s not that difficult to do and involves no Apex code. Second, it uses 100% lightning supported features which means it works in Mobile. Third, because we’re using Flow and Lightning Components, we may be able to do other things besides creating new records.

More Ability

This currently works ONLY for new records. I tried to create a URL Hack so we could update an existing record. The URL Format is a bit different: /lightning/r/Contact/0030M000028DAvKQAW/edit?

Note that instead of an “o” it’s an “r” before the object. We also have the record Id and the word “edit” instead of “new.” I was hoping by simply adding defaultFieldValues= after the the question mark I could add more information. For example /lightning/r/Contact/0030M000028DAvKQAW/edit?defaultFieldValues=description=Test%20Hello This doesn’t work.

So we can only create new records.

Mobile

Lightning is very handy because it’s now the system that drives our mobile platform – especially in the Spring 20 release.  I created a URL hack that simply defaults the name of the account and the description.  I took my sample URL Hack and tried in mobile.

My result:

 

URL Hack Spring 20 in Mobile

URL Hack Spring 20 in Mobile

Not exactly what I call friendly. Since the Lightning buttons/actions and the mobile buttons use the same set up on the page layout – you’re stuck. This is especially true if you have users that use both Desktop and Mobile and cannot create a mobile only page layout.

The Flow/Lightning Component Action solution does work in mobile.

More Functionality

This is a bit redundant with the first option. The fact is because in my example we use flow and Lightning Components, we can create a whole bunch of cool automation. We could create a “hack” that sends email, creates records, update records, does a bunch of stuff and navigates us to a new page. Why we would want to limit ourselves to a URL only is mystifying to me now. I understand it’s easier for lots of folks to wrap their head around, but the automation platform is going to Flow. It’s something you’re going to want to learn and should learn.

For me, the new URL Hack functionality in Spring 20 is a short cut. If your only need is to create a new record, default field values, and a action isn’t good enough – go right ahead and use it. But if the main reason you’re doing it is because you don’t think you can handle using a Flow with some Lightning Component actions – then stop. Take the time to learn how to do it. The URL Hack may be an acceptable short-term, “I need this out right now” solution, but you’ll be closing your off to the amazing capabilities you could do with just a little education and imagination. Plus, who wants something that only works in desktop and fails in the mobile world these days?

Don’t miss a post! Sign up to receive email notications.

Subscribe

Subscribe to our mailing list

* indicates required
I want to receive email notifications about:

I run this site, the podcast, and my training videos for the community. I do incur some costs. I greatly appreciate if you can help me out by checking out my affiliates and – if you shop amazon – start your search on my site.

Recommended Posts
Showing 16 comments
  • Chris Pearson
    Reply

    I have been waiting for an explanation on the “why” for this feature – especially given the age of Lightning and how we should have been avoiding URL hacks for years. Thank you, Brian, for a great article!

    • Brian Kwong
      Reply

      I think the “why” has been a lot of people wanting it and complaining.

      I do like their method of making it more “Standard.” No more going to find HTML IDs for example.

      But I would preferred if they released some standard lightning components that we could re-use. Like Navigate to an sRecord, create/edit new record where we’re passing in the values. The defaultFieldValues is actually one of the attributes in the Lightning Component to create new records, so that may be exactly what they’re doing, using a lightning component but providing access to an attribute via a URL Parameter. But I prefer to create a custom link, custom button, custom action, or hyperlink formula that is accessing that component directly and letting me pass the attributes in. I think it’ll be more flexible in the long run. But I’m not a product manager.

  • Dhana
    Reply

    Hi, Trying to understand if the below URL hacking is doable through the Lightning. We have a custom button in Classic which calls the list view of Custom object like (where fcf is the view on the Custom Object type a3Y)

    /a3Y?fcf=00BU000000XYZ28&rolodexIndex=-1&page=1

    • Brian Kwong
      Reply

      Dhana,

      From what I can tell, the only URL hacks for Lightning that are supported are filling in report parameters and navigating to a NEW record page.

      What’s your business case for the custom button? Since we can put list views on the lightning page, I would add a new tab (assuming this is a record page) and have that tab with the list view component.

  • João Silva
    Reply

    Hey great article! I currently creat an object in edit mode as you described above, but after i save it it doesnt return to the object’s page in view mode. any tip on how to do this?

    • Brian Kwong
      Reply

      So you’re saying you’re editing a record, click a button create a new record, and you expect to see the view of the starting record?

  • Loren
    Reply

    Brian!
    I am doing a lightning hack to create a new opportunity from the account. However, I can’t get the account name lookup to be filled in on the new opportunity record creation! What am I doing wrong?

    /lightning/o/Opportunity/new?defaultFieldValues=
    AccountId = {!URLENCODE(Account.Id)}

    • Brian Kwong
      Reply

      Loren, a few thoughts, you shouldn’t need to URLEncode a Salesforce id since it’ll only ever contain numbers and letters. THat’s really for things that have spaces, commas, apostrophes and so forth.

      Also, I’m a big fan of using a fully baked URL for testing outside of a button or link. You can simply paste your URL into the browser, see how it works, and make changes until you get something to work. Unfortunately, salesforce doesn’t always show you the resulted URL when it’s called from a button. It does show it when you call it from a link but it quickly changes to some nonsense.

      For example. Past Thewizardnews.lightning.force.com/lightning/o/Opportunity/new?defaultFieldValues=AccountId=001998asdf7889 — obviously use your Instance name and not thewizardnews and actual account id.

      And see what happens. I got it to work doing it this way. The only thing to know is you can’t have any spaces in the URL so AccountId = IDvalue is wrong. AccountID=IdValue is correct.

      Again, I only recommend using URL hacks like this if you NEED to have the full page layout when creating a child, or if you’re creating a record that is not a child of the starting record. A custom create action is simply better and easier in those use cases.

  • John
    Reply

    Been struggling to find a way to use these with other formula elements all afternoon and have come to the conclusion that it isnt happening I think. For example, if you wanted to do {!IF(abc=abc, , other url,)} you need to be able to put something surrounding the lightning hack to use it in the IF (as it starts with a / and has commas in it). As soon as you do that, it blows up when you press the button by trying to encode it (or something) and either takes you to an error page in lightning in a new tab for some reason, or the home page. It looks to me like the parameters included after defaultFieldValues are not included in the URL itself so having them within, for example quote marks, is putting them in the URL which seems to not be how this works.

    Anyone had any luck with something like this?

    • Brian Kwong
      Reply

      I’ve had luck. The way lightning works makes it difficult to test and debug these because the URl doesn’t actually show up at the final result screen.

      If I use a link instead of a button I have seen the URL show up for a short period of time before the browser moves to the next URL which is the final result. I’ve had some success grabbing the URL and seeing how the link/button is being resolved to figure out problems.

      The other thing you can do (in a sandbox obviously) is to take your formula and create a formula field. You won’t be able to use it as an actual link, but you can see how your functions and formula resolve for that record.

      Keep in mind you only need to URLEncode the bits that could have things problematic (spaces, apostrophes, etc) and you don’t have you URLEncode the whole URL string.

  • Glenn Seidenfeld
    Reply

    Hello Brian,
    Is it possible to set a default value for a field as a specific account record that acts as a placeholder? lightning/o/Account_Referral__c/new?defaultFieldValues=Lead__c={!Lead.Id},client__c=specific record here

    • Brian Kwong
      Reply

      You could hard-code a record ID (assuming client__c is a lookup field). However, I don’t recommend it since if anything happened to that record, you’ll break your button.

      unless your value for client__c is coming from the record so you can grab it via a field merge

  • Andy
    Reply

    The only reason I want to implement this at all is because the standard “New” button on a related list doesn’t fill in values in Lightning as they did in Classic.
    Does this work with list buttons in a related list? I’m not having any luck.
    From parent Opportunity, trying to have a “Make me a new Quote” button on the Quotes related list. And have it fill in the Opportunity lookup, Account lookup, and Sales rep lookup on the new Quote record.
    (Alternative is to hide that new button and instead retrain users to use Actions. But sometimes the new button comes back….)

    • Brian Kwong
      Reply

      I haven’t had any luck with it on related lists.

      However, I’ve been able to do Flows (with local action) to replicate the functionality. No coding involved. This walks through the process https://thewizardnews.com/2018/08/02/url-hack-functionality-lightning/

      I’ve also used straight Lightning Component but I have found some odd foibles to it when users click, exit out, and then attempt to click it again. That can found as a post by Doug Ayers – linked at the bottom of the post I listed above

  • Silvia
    Reply

    I need to replace a URL hack we build to open a new email screen with a pre-determined template (based on a formula field). Any suggestions? This is our classic button:
    /_ui/core/email/author/EmailAuthor?p2_lkid={!Enrolment_Form__c.Student_NameId__c }&p3_lkid={!Enrolment_Form__c.Id}&template_id={!Enrolment_Form__c.AUS_DOM_Offer_Template_ID__c}&p24={!Enrolment_Form__c.Student_Email__c}&new_template=true&p26=enrolments@sae.edu&retURL={!Enrolment_Form__c.Id}

    • Brian Kwong
      Reply

      Lightning URL hacks only are supporting creating new records. So it doesn’t support the email setup at all. The email system is completely different in Lightning so you wouldn’t be able to update it. I don’t think there’s a LEX support method of doing emails. The closest I could get you is an action that calls a flow & send an email alert. That would mean that the user couldn’t modify anything in the email and you would have to have a field to store the recipient of the email on the record you’re clicking the button from.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.