Monday, August 13, 2018

Component Events: Way to Communicate Between Components in Containment Hierarchy

Components events can be used to pass information between lightning components which are in containment hierarchy. Component events can be handled by component which is firing it or by component container.

Through this blog, I am going to explain how to utilize component events with sample code.

I have created a sample lightning app which contains "c:SK_MasterCmp".  "c:SK_MasterCmp" contains another component "c:SK_ChildCmp"

Below are steps which you need to follow while utilizing component events:

  • Create Lightning event of type "COMPONENT".
        <aura:event type="COMPONENT" description="Event template" >
              <aura:attribute name="passedValue" type="string"/>

  • Register the component event which is going to fire component event.
        <aura:registerEvent name="changeValueEvent" type="c:SK_LightningEventChannel"/>
  • Use below syntax to fire event and pass information from "c:ChildCmp".
        var newEvent = component.getEvent("changeValueEvent"); 
        newEvent.setParams({"passedValue":"twitter handle-@sunil02kumar"});                              ;
  • Create handler in component which needs to handle the events means want to consume that information. Note the name of handler should be same as that while registering event:
       <!--name should be equal to name while register event-->
       <aura:handler name="changeValueEvent" event="c:SK_LightningEventChannel" 
  • Create function in controller.js which will be called when "c:MasterCmp" will handle the the event.
       handleNotification : function(component, event, helper){
           console.log('****handling event');
           var sentMessage= event.getParam("passedValue");
           component.set("v.mastervalue", sentMessage);

Below is complete code for reference:

Hope this help!!

Thursday, August 9, 2018

How to get User Session Id in Future Method or Batch Class

Sometime it is required to perform Callout in future method and Batch apex class. If we use UserInfo.getsessionId() method inside the future method or batch apex, then it will return null.

Below are different option to get sessionId:

Option 1:

We can either pass sessionId as parameter to future method or to batch apex class.

Option 2:

Create a VF page and use {!$Api.Session_ID} in body.  In apex, get VF page content which will contain session Id.

Below is code snippet explaining the Option 2 by using future method. Same can be used in Batch Apex.

Run below code snippet in execute anonymous to check the output in debug logs:


Hope this help!!!

Remote Site Settings : Way to Create/Update Using Apex (Metadata API)

We can create or update Remote Site Settings in Apex using Metadata API.

You can either download Metadata API WSDL from Salesforce and generate apex class by clicking on Generate from WSDL button.

Or you can download the MetadataService class from below link:


Below is sample to code to create remote site settings.

Execute this static method from execute anonymous and check if records created or not.


Below is snapshot of remote site settings created

  • I have used upsertMetadata method. If you want to only insert new remote site setting, then use insertMetadata method
  • fullname property is considered in order to decide whether remote site setting needs to be created or updated.
Hope this will help!!!

Create Custom Metadata Types Using Metadata API
Create Update Custom Label by Using Metadata API