Monday, May 13, 2019

Prepopulate Lookup in Lightning record edit form when it override to New button in Service Console

In this article, we would learn how can we auto-populate with parent account when creating a new child record from the related list of parent.

In Salesforce when you create a master-detail relationship between two objects and whenever you create a child record from the parent related list, the parent record automatically auto populate lookup field of its parent when you try to create the child record.
But if you override the child object's New button with some custom lightning component and try to create a new child record in this case the parent record does not automatically auto-populate the lookup field of its parent.

So in this article, we would learn how to automatically auto-populate the lookup field of its parent.

For example, we have Account and Address object, Account is the parent and the Address is the child object, also I override Address component's New button with the custom lightning component in which I am using lighting record edit form to create the new record.

Here is the code for the lightning component

LightinigRecordEditForm.cmp

  <aura:component implements="lightning:actionOverride,force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction"  access="global" >
    
    
    <!-- Attributes --> 
    <aura:attribute name="parentRecordId" type="String" />   
    <aura:handler name="init" value="this" action="{!c.doInit}"/> 
    
    <lightning:card iconName="standard:account" title="New Address" class="">
        <div class="slds-p-left_large slds-p-right_medium"> 
            <lightning:recordEditForm
                                      onload="{!c.handleLoad}"
                                      recordId="{!v.recordId}"
                                      onsubmit="{!c.onSubmit}"
                                      onsuccess="{!c.onSuccess}"
                                      onerror="{!c.onError}"       
                                      objectApiName="Address__c">
                <lightning:messages />
                <h3 class="slds-section__title slds-theme--shade primaryPaletteBorder test-id__section-header-container" >
                    <span class="test-id__section-header-title section-header-title slds-p-horizontal--small slds-truncate" >Address Information</span>
                </h3>
                <div class="slds-grid">
                    <div class="slds-col slds-size_6-of-12 slds-p-around_small">                 
                        <lightning:inputField fieldName="Name" aura:id="addressName" />
                    </div>
                    <div class="slds-col slds-size_6-of-12 slds-p-around_small">
                        <lightning:inputField fieldName="Account__c" value="{!v.parentRecordId}"/>
                    </div>
                </div>
                
                
                <lightning:layout horizontalAlign="center" class="slds-m-top_large">
                    <lightning:button variant="neutral" label="Cancel" title="Cancel" type="text" onclick="{!c.onCancel}"/>
                    <lightning:button variant="brand" type="submit" name="save" label="Save" />
                </lightning:layout>
            </lightning:recordEditForm>     
        </div>
    </lightning:card>
</aura:component>
  
LightningRecordEditFormController.js

  ({
    doInit: function(component, event, helper){
     
        var parentId = helper.getParentId(component , event, 'inContextOfRef');
        var context = JSON.parse(window.atob(parentId));
        component.set("v.parentRecordId", context.attributes.recordId);
   
    },
    onSuccess : function(component, event, helper) {
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            "title": "Success!",
            "message": "The record has been Saved successfully."
        });
        toastEvent.fire();
    },
    onSubmit : function(component, event, helper) {
    },
    onLoad : function(component, event, helper) {
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            "title": "Loaded!",
            "message": "The record has been Loaded successfully ."
        });
        toastEvent.fire();
    },
    onError : function(component, event, helper) {
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            "title": "Error!",
            "message": "Error."
        });
        toastEvent.fire();
    }
 
 
})
  
LightningRecordEditFormHelper.js
({
  getParentId: function(component, event, name) {
        name = name.replace(/[\[\]]/g, "\\$&");       
        var url = window.location.href;     
        var regex = new RegExp("[?&]" + name + "(=1\.([^&#]*)|&|#|$)");
        var results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }
})
  

No comments:

Post a Comment