URL Hack in Lightning, this is enough to get us excited. With the introduction of Lightning Experience, the most missing feature was URL Hack in Lightning Experience. With Spring 20 this feature has come back officially and with each release, it gets fixes and updates. So today we will cover this and we will check how you can easily use it to prepopulate value.
I have also covered this topic in past Default Field values using Lightning Web Component but I thought that we should cover this in detail.
Clone Record in Lightning Experience
Previously in Clone record, we provide clone=1. So we can follow the same approach in the Lightning Experience URL button as well. For Example, We don’t have a Clone button on Account. So we will create a clone button for Account in Lightning Experience.
Create a new URL button in Account and enter the below URL.
/{!Account.Id}/e?clone=1
Now add this button in Layout and our Clone button is ready.

URL hack in Lightning Experience/ Prepopulate Field
So now we will create another button. For Example, We will create Task with prepopulate data from Account. We will check How to set RecordType, How to populate Date, DateTime, Boolean, and other fields.
If you want to try it in your org, use below sample code:
/lightning/o/Task/new?useRecordTypeCheck=1&defaultFieldValues=
WhatId={!Account.Id},
WhoId=null,
ActivityDate={!TEXT(Account.Meeting_Date__c)},
IsReminderSet=true,
Meeting_Time__c={!TEXT( DATEVALUE(Account.Activity_Date__c) )}T{!TEXT( TIMEVALUE(Account.Activity_Date__c) )}Z,
Subject={!URLENCODE('From Account')}&
backgroundContext=%2Flightning%2Fr%2FAccount%2F{!Account.Id}%2Fview
Now we will cover each attribute which we pass here.
- useRecordTypeCheck: We use this attribute to display RecordType Selection page.
- recordTypeId: if we have recordType Id then we can pass directly, other attributes will remain the same. This is case sensitive. /lightning/o/Task/new?recordTypeId=012B0000000YOIg3&defaultFieldValues=
- DateTime Field: Salesforce support given format. So we first separated DateTime into Date and Time and then use Text to convert it as String. I have also hardcoded T and Z format.
- Date Field: For the Date field, we use the TEXT method.
- Text and Other Field: For Text and other fields we can pass them using URLENCODE method.
- backgroundContext: This is used to set the background, when user clicks on cancel, he will lend on this page.



One thing you have noticed that in WhoID field I pass null value. Before Summer 20 this throw an error but this is fixed now. If your org is still in Spring 20 then you can use below alternative.
{!URLFOR('/lightning/o/Opportunity/new?defaultFieldValues=' + IF(ISBLANK(Contact.AccountId), ", ',AccountId='+Contact.AccountId) )} //use URLFOR to genrate dynamic URL and pass variable only when they have value
So now we can easily use URL Hack in Lightning and set the values for different fields. Previously we need to create our custom form for this but now we can skip that.
Read Also: How we can create and setup Dynamic Forms and Dynamic Actions.
Like my Facebook Page for more updates.
Did you like the post or do you have any question, let me know in comments. Happy Programming 🙂
Hi, there. On the Date time field, why do you need to hard code the letter ‘T’ and ‘Z’ ? Thanks in advance.
Its standard format which Salesforce support
Hi,
I am trying to utilize the clone functionality, but change the values of a couple of fields; primarily, Status fields that need to change, or date fields. Is this possible using a similar defaultFieldValues property? Or can we only utilize Lightning URL Hacking to create an exact clone?
Thank you
Hi,
We have limitation here. You need to pass JSON with all fields pre populate, if you want to change few fields only. But it is possible.
Hello Everyone,
Does URL hacking works with custom fields on Case or is there any limitations?
I have been working on it, but running into issues,
USECASE:
I need to pre-populate child case fields from parent case and select record type,
/lightning/o/Case/new?useRecordTypeCheck=1&defaultFieldValues=
CaseNumber={!Case.CaseNumber},
ParentId={!Case.ParentId},
abc_first_name={!Case.parent_First_Name__c}
abc_first_name= first name on child case
parent_First_Name__c = first name of paren case
any help is greatly appreciated
Your code should work. Are you getting an errors?
I am trying to do an URL hack by pre-populating the recordtype. The layout name shows the default record type’s layout name instead of the the one mentioned in the button , however the layout is rendered correctly. Anyone faced this issue and any idea on how to resolve that ?
Yes I faced this before. Make sure you are using the exact same code as above or try this: ?recordTypeId=012XXXXX&defaultFieldValues=RecordTypeId=012XXXXX,
Thanks Tushar -and it is as Tushar typed -recordTypeId – yes it is case sensitive. And i did not need the second value – just the one before &defaultFieldValues
How we can get prepopulate multiselect picklist value for cloning record using urlFor, custom button
Unable to prepopulate while clicking multiselect picklist value using Urlfor in lightning. can you please tell how i can show that mulit select picklist value using custom button only (without lightning component) Url hack
You can use below URL in URL button. Paass value using semicolon seperated.
/lightning/o/Account/new?defaultFieldValues=
Demo_MSPL__c=Demo 1;Demo 2&
backgroundContext=%2Flightning%2Fr%2FContact%2F{!Account.Id}%2Fview
Tushar,
Nice post. I have my button working well. One modification I would like to make is with backgroundContext. I have the Account DETAILS tab as the default tab displayed when I load an account page. Can I get the background context to stay on RELATED tab, or is that not possible using URL hack.
Thanks,
Mike
I believe it will consider the default tab in your case that’s detail tab.
Hey Tushar,
Thanks for the reply. 2 other questions.
1. Noticed if one of the fields you are trying to assign default values for contains commas, the button errors out, believing the commas are separating out new custom fields. Do you know if there is any way to handle this?
2. I am also having trouble getting lookup fields to populate via the URL hack. Do you know why that might be?
Have my code below: ightning/o/Opportunity/new?useRecordTypeCheck=1&defaultFieldValues=Account={!Account.Id},Single_Sign_On_Provider__c={!Account.Single_Sign_On_Provider__c},MDM_System__c={!Account.MDM_System__c},Ticketing_System__c={!Account.Ticketing_System__c}&backgroundContext=%2Flightning%2Fr%2FAccount%2F{!Account.Id}%2Fview
Thanks for the help
Mike
For lookup it should be AccountId. For comma you can encode that data.
Appreciate if you could provide an example of how you would encode it? Yeah I have Account.ID there, just does not seem to be working.
Mike its AccountId and not Account.Id. you can use {!URLENCODE()} to do the encoding.
Got it. The issue actually was I was using Account={!Account.Id}. Need to use AccountId={!Account.Id} and the lookup populates upon click
Hi, Tushar I created a link that takes me to create “new record form”, when I click on cancel it just leaves a blank page. I used backgroundContext in my URL but for some reason it doesn’t do anything.
/lightning/o/A_133_Audit_Report__c/new?recordTypeId=”012t00000004HxiAAE”&defaultFieldValues=Organization_Name__c={!Account.Id}&backgroundContext=%2Flightning%2Fr%2FAccount%2F{!Account.Id}%2Fview
Could you help me to figure out why it is not working?
Thank you
Try to use current Audit Report record Id in background Context.
Hi I’m trying to set a predefine value on my custom clone task button:
here’s my syntax and it is not working
/{!Task.Id}/e?clone=1&defaultFieldValues=Status=”In Progress”
Change Status=”In Progress” to Status={!URLENCODE(‘In Progress’)}
Error:
“Looks like there’s a problem.
Unfortunately, there was a problem. Please try again. If the problem continues, get in touch with your administrator with the error ID shown here and any other related details.
Error ID: 884914470-165751 (-388881258)”
Url:
/lightning/o/WorkOrder/new?&navigationLocation=RELATED_LIST&recordId={!post__c.Id}&defaultFieldValues=ref_relatedPost__c={!post__c.Id},
AccountId={!post__c.ref_relBusinessPartnerId__c},
Subject={!post__c.txt_subject__c},
Description={!post__c.ltx_content__c}
Getting error if “ref_relBusinessPartnerId__c” value is not set. If it set then the code works fine. Would you please tell me why i am gettings this error
Can I specify which tab it should go back to instead of default tab?
How can I go to a non default tab using the background context?
“/lightning/o/esOpportunity__c/new?clone=1&recordTypeId=0128K0000004KKdQAM” I am using this and creating a new record but after checking record weather it is cloned or nor by using isClone() then it was returning false i.e. not cloned.
I am trying to do a URL hack on the clone button for event. When I try to default the value for owner ID it still defaults it to the user (which is not what is defaulted). Is there a way around this?
for owner it take current user. Are you passing 15 or 18 character Id?