August 12, 2020

How to get default field values set in URL in Lightning Aura Components in Salesforce?

 Sample Code:

Component:

 <aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,lightning:actionOverride,lightning:isUrlAddressable" access="global" >
   
    <aura:attribute name="recordId" type="String"/>
   
    <lightning:notificationsLibrary aura:id="notifLib"/>
   
    <div class="slds-box slds-theme--default">
       
        <lightning:recordEditForm objectApiName="Lead" onload="{!c.handleCreateLoad}">
            <lightning:messages />
            <lightning:inputField fieldName="FirstName" aura:id="FirstName"/>
            <lightning:inputField fieldName="LastName" aura:id="LastName"/>
            <lightning:inputField fieldName="Email" aura:id="Email"/>
            <lightning:inputField fieldName="Company" aura:id="Company"/>
            <lightning:inputField fieldName="LeadSource" aura:id="LeadSource"/>
            <lightning:button class="slds-m-top_small" variant="brand" type="submit" name="Create" label="Create" />
        </lightning:recordEditForm>
       
    </div>
   
</aura:component>

JavaScript:

({
   
    handleCreateLoad : function(cmp, event, helper) {
       
        var myPageRef = cmp.get("v.pageReference");
        console.log( 'Pagereference is ' + JSON.stringify( myPageRef ) );
        console.log( 'Default Values are ' + JSON.stringify( myPageRef.state.defaultFieldValues ) );
       
        if ( myPageRef.state.defaultFieldValues ) {
           
            myPageRef.state.defaultFieldValues.split( "," ).forEach( function( record ) {
               
                var recordSplit = record.split( "=" );
                cmp.find( recordSplit[ 0 ] ).set( "v.value", recordSplit[ 1 ] );
               
            });                        
           
        }
       
    }
   
})

Button Override:

 Output:

Use this path to test it - /lightning/o/Lead/new?defaultFieldValues=FirstName=Test1,LastName=Test2,Email=test@dd.com,Company=Testing

 


No comments:

Post a Comment