+ Start a Discussion
Jun TeppangJun Teppang 

Spring 20 - Navigate to a Record’s Create Page with Default Field Values - Return URL not working

As of Spring 20 release, we can again prepopulate field values using a URL - aka URL hacking (see release note here (https://releasenotes.docs.salesforce.com/en-us/spring20/release-notes/rn_general_lex_navigate_to_record_dfv.htm)).

I, for one, am so happy for this feature. However, there is one minor problem. When the URL is invoked, the new record modal pops up, and if the user chooses to Cancel, a blank page is displayed instead of returning to the page where the URL was called.

Below is the URL that I'm using. It is invoked with a custom list button located in a related list of an Opportunity.

/lightning/o/Case/new?defaultFieldValues=
AccountId={!Opportunity.AccountId},
Opportunity__c={!Opportunity.Id}

I've tried including a retunURL parameter - in different places in the URL. But no luck.
Jun TeppangJun Teppang
If anyone is intereseted, I think I've figured out a solution to specify a return URL for this. And as an added bonus (thanks to 
Iqbal Hossain 1 (https://developer.salesforce.com/forums?feedtype=RECENT#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=ALLQUESTIONS&id=9062I000000XrIqQAK)) you can also specify the record type id:

Here is the updated sample URL (reminder that this is invoked by a custom link button located in a related list of an Opportunity record):

/lightning/o/Case/new?recordTypeId=<insert your record type id here>&
defaultFieldValues=
AccountId={!Opportunity.AccountId},
Opportunity__c={!Opportunity.Id},
ContactId={!Opportunity.Contact__c}&
backgroundContext=%2Flightning%2Fr%2FOpportunity%2F{!Opportunity.Id}%2Fview



And for those using lighting/navigation in LWC, here is a sample function code:
  this.defaultValues = `AccountId=${this.accountId},Opportunity__c=${recordId},ContactId=${this.contactId}`;  
​​​​​​​
  . . .

  navigateToNewRecord() {

        this[NavigationMixin.Navigate]( {
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Case',
                actionName: 'new',
            },
            state: {
                recordTypeId: <insert your record type id here>,
                defaultFieldValues: this.defaultValues
            }
        });
    }
Note that you don't need the "backgroundContext" parameter.

Hope this helps someone out there!

 
Craig Maxwell 37Craig Maxwell 37

A slight aside however, for record types you can also use this method to envoke the record type selector:

useRecordTypeCheck=1
I found this very handy, just wanted to share and hoping it might help others stumbling accross this thread.
Vijay VadgamaVijay Vadgama
Hi Craig / Jun,

I was wondering if someone can help me?

I'm actually having an issue when trying to save the record (pressing "Cancel" on the record is fine). 

Let me give you some background:

As we're transitioning to Lightning, the business is requiring Lead information to be populated to activities (which can't be done using Actions). I've created a URL hack for "New Task" which displays near the buttons on the Lead lightning page. Pressing the "New Task" button (and cancelling) is working fine, but when I try to "save" the Task record, it re-directs me to the Task page, despite having &retURL at the end of my hyperlink. 

I've tried every possible solution I can think of, but this issue still persists. Can somebody please help me if they know how to return to the record the button was pressed on? (in my case, Lead)

My URL example:
/lightning/o/Task/new?defaultFieldValues=
{!IF(ISBLANK(Lead.AccountId__c),'','WhatId='&Lead.AccountId__c)},
{!IF(ISBLANK(Lead.Primary_ContactId__c),'WhoId='&Lead.Id,'WhoId='&Lead.Primary_ContactId__c)},
Lead_Lookup__c={!Lead.Id}
&retURL=%2F{!Lead.Id},
backgroundContext=%2Flightning%2Fr%2FLead%2F{!Lead.Id}%2Fview


Regards,
VJ
Jordan Wight 7Jordan Wight 7
@Jun - Is this working only for a custom link? I tried this using a custom list button, and it's not working for me.
Lee Anne GLee Anne G
We too, had a related list and a custom button to add a new Opportunity Contact Role. Once we switched to lightning the default value (the opportunity this new Contact was related to) and the Cancel button were no longer working as suspected. Here's the syntax I used to fix it to work in lighting. ** Please note that our Opportunity object and it's related Opportunity Contact Roles object are both custom objects.

{!URLFOR('/lightning/o/Opportunity_Contact_Role__c/new?defaultFieldValues=Opportunity__c='&Opportunity__c.Id+'&backgroundContext=%2Flightning%2Fr%2FOpportunity_Contact_Role__c%2F'&Opportunity__c.Id+'%2Frelated%2FOpportunity_Contact_Roles__r%2Fview')}

Now, when we're on the related Opportunity Contact Roles list and we click the Add Additional Contact button, the Parent Opportunity is there by default and if we hit cancel, it's no longer blank. :-)