Sunday, May 26, 2019

How to use custom metadata types in the lighting flow.

Hello guys, In this article, we will learn how can we use Flow builder, Process builder, and Custom metadata types to update records.

I have a scenario in which I have one custom metadata type named as STATESCITIESPINCODE which contains pin codes of corresponding cities with their states, now whenever a user creates the records he enters state name and city name so our process builder calls flow which having logic how to get pin code corresponding to state and city to enter by the user while creating the record and then our process builder update the record with the pin code.

so first let's create the custom metadata type which having the states, cities, and corresponding pin codes,

So go to Setup >> Enter custom metadata in the search box >> click on Custom Metadata Types under Develop
so I have custom metadata types with three custom fields City, State, and Pincode



now we'll create the flow
so go to setup >> Enter Flow >> Click on Flows under Process Automation

So in our flow, first of all, we'll create the variables for this click on Manage on the left and then click on New Resource then the popup will come in the search box enter variable and click on the variable


Repeat the steps for creating the three variables varCity, varState, varPincode, and varId of datatype Text.
Create another variable varUpdateCityRecord with datatype as Record and Object type is as City, which will be used to update the record



Create another variable varGetPincodeRecord with datatype as Record and Object type is as StatesCitiesPincode( which is our custom metadata object), this will be used to store the value of pin code.




Now we have created the variables now we'll search for the records in the custom metadata type. so for this click on the element on the left and drag the GetRecords under data, a popup will come, give a suitable label, in the object search your custom metadata type name, in the condition requirement select Conditions are met







Now Click on Assignment under the Logic and drag it to right again the popup will come, give some Label then set the variable values



next and final step is to update the Record variable, for this click on the Update Records and drag it to the right again popup will come, give the suitable name and then in the select variable select the varUpdateCityRecord variable which will update the Record with the pin code.





join all them together and the finally your flow will look like this


Save the flow as autolauchedflow, and don't forget to activate it.

Now create the process builder which will run when we try to save our city Object record and will call the flow to update the record with the corresponding pin code

Select City as an object, in the criteria, select No criteria—just execute the actions! 


and in the immediate actions  Select Flow as an action, give an action name and select your flow which you just created and provide values to the flow variables



Save your process builder and don't forget to activate it

Now everything is done try to create the City Record give postcode and state values and click on save and your record will be updated with the corresponding pin code.

That's it guys,


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, " "));
    }
})