Redirect To Custom Page after Record Creation in Lightning


As we know that in Salesforce Lightning there is no direct way to override Save button and redirect to new custom page after creating record from Standard Record creation page.
This blog may help you redirect to new Custom Lightning Page after creating record.
Requirement: 

I want to redirect to a custom page after creating any record. Redirection should not work if record is getting updated.

Solution 1: 

If Page Layouts do not have lot of fields then create new Lightning Component with Custom Save Button. But cannot use Lightning Data Service because LDS does allow to override Save Button.

Solution 2:

But what if Page Layouts have 100s of fields. Now it's not possible to create a Lightning Component and add all fields on that Lightning Component.
For this situation we have a work-around. Below are the steps need to perform for this requirement:


  • Create a custom Field. Let's call it isCreated. Data Type - Checkbox
  • By default value of this True. Why true? - We will understand this later in the same blog.
  • All profiles should have access and this field will not present on page layout.
  • Now it's time to create a Lightning Component. 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<!--RedirectLightningComponent-->
<aura:component implements="lightning:isUrlAddressable,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
 <!--ATTRIBUTE-->
    <aura:attribute name="recordId" type="String" default=""/>
    
    <!--EVENT-->
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
    <!--LIBRARY-->
    <lightning:navigation aura:id="navService"/>
</aura:component>

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
({
 doInit : function(component, event, helper) {
  var pageReference = {
            type: 'standard__component',
            attributes: {
                componentName: 'c__CustomRedirectPage',//Mention your Component where you want to redirect.
            },
            state: {
                "c__accountId": component.get('v.recordId')
            }
        };
        component.set("v.pageReference", pageReference);
        var navService = component.find("navService");
        navService.navigate(pageReference);
 }
})
  • After creating this component place this component on Record Detail Page. Because Body is empty so it will not impact your existing Page layout.
  • After placing this component, set the visibility criteria. If isCreated Checkbox is true then only make it visible. Else it will be invisible. Even if this component is visible User will not able to see anything on this component because body of this component is blank. 
  • Now every time when user will create new record, because isCreated checkbox will be checked so this new component will be visible on layout (not visible to user) and on execution on doInit method it will redirect to custom Lightning Page.
  • Once custom Lightning Page (Lightning Component) will open in doInit method it will call the apex class and update the isCreated checkbox true. So next time when record detail page will open it will not redirect to custom lightning page.
  • Now we marked isCreated checkbox true because if we mark it by default false then for all the existing records also we need to mark this checkbox true so that for existing record it should not redirect to custom lightning page.
  • Once main Lightning page (Component) will open. It will execute doInit method and call the apex class to update isCreated checkbox false. So that next time when user will open the Record detail page it will not redirect to this custom page. 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<!--CustomRedirectPage-->
<aura:component implements="lightning:isUrlAddressable,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
 <!--ATTRIBUTE-->
    <aura:attribute name="accountId" type="String" default=""/>
    
    <!--EVENT-->
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    
    <!--BODY-->
    Hello This is redirected Page after record Creation.
</aura:component>

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
({
    doInit : function(component, event, helper) {
        var action = component.get("c.isNewRecord");
        action.setParams({ 
            recordId : component.get("v.accountId") 
        });
        action.setCallback(this, function(response) {
            
        });
        $A.enqueueAction(action);
    }
})

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
/*************************
* Class Name - RedirectController
* CreatedBy - FlightToSalesforce
* Date - 7th December 2019
* Description - Handle Redirections after record creation.
**************************/
public class RedirectController{
    @auraEnabled
    public static void isNewRecord(String recordId){
        Account acc = new Account(Id = recordId, isCreated__c = false);
        Database.update(acc);
    }
}

Comments

Post a Comment

Followers

Popular posts from this blog

Salesforce LWC: Multi-Record Creation using Lightning Web Component

Salesforce LWC: Basic Drag and Drop Functionality

Salesforce Lightning Web Component: Dynamic Column Selection in Lightning Datatable