Friday, August 24, 2018

Way to get list of Scheduled reports and Jobs from Salesforce

This is very common ask from business admins or end users to get list of schedule reports or schedule jobs (batch jobs, scheduled jobs etc) from Salesforce.

There is no standard view provided by Salesforce to view list of scheduled reports or reports. Users have to navigate to Monitor section under SetUp.


This section display list of jobs either schedule or completed jobs but does not provide complete list of jobs.

By using apex, you get the list of schedule reports or jobs in csv format via email. 

Please find below the apex code for that:


Now if you have to get list of scheduled reports in your org, then just execute below script in developer console:

  • For getting list of scheduled reports:         SK_ScheduleJobUtility.findScheduledJobDetails('Report Run');

  • For getting list of scheduled batch jobs:         SK_ScheduleJobUtility.findScheduledJobDetails('Batch Job');

  • For getting list of scheduled apex jobs:        SK_ScheduleJobUtility.findScheduledJobDetails('Scheduled Apex');


Important Use case

Sometime reports are scheduled by user which becomes inactive, then all system admin start getting email saying:

"The user account that runs the report (the running user) is inactive."

This utility will send email along with user details which will help in identifying all inactive user for whom reports are scheduled as running user.


Hope this will help!!

Looking forward for everyone's comments and feedback.

Monday, August 20, 2018

Creating Lightning Components Dynamically

Through this blog, I will share the approach to create lightning components dynamically by sharing an example in which we will display account summary information by creating summary component dynamically.

I have already posted one blog through which we can create ui:button dynamically and can destroy it. Please refer below URL if you want to refer that before continuing with this blog:

creating or destroying buttons dynamically

Below is snapshot of app which will display recent accounts on left side and on right side, it will display opportunities and case summary for account. You can click on "show Info" link before any account record and its related information will appear on right side dynamically.


Code to create dynamic component

You can use $A.createComponent method which accepts 3 parameters:
  1. ComponentName (String) :Type of component to create like "c:newCmp" etc.
  2. attributes : JSON specify component attributes values
  3. callback function: Once the component is created, this callback function will be invoked.This function can be used to append newly created component to target component. You can place the newly created component inside some div to display.
Below is sample code along with comments to explain steps:

 //find the div markup where you want to display dynamic component
//here "dynamicCmpSection" is aura:id of div
var targetCmp = component.find("dynamicCmpSection");
//create JSON specify all attributes needed for new component
var cmpAttributes = {
                "attribute1": "attribute Value",
"attribute1": "attribute Value"
            }
$A.createComponent(
"c:Demo_DynamicViewCmp", 
cmpAttributes,
function(tempbody) {
//Check if component is valid
if (component.isValid()) {
var targetCmp = component.find("dynamicCmpSection");
//first remove all content inside the div by making its body as null
targetCmp.set("v.body",[]);
var body = targetCmp.get("v.body");
//add newly created component in div
body.push(tempbody);
targetCmp.set("v.body", body);
console.log('***component loaded successfully');
}
}
);

Below is complete code related to snapshot shown in blog.

Hope this Helps!!

Sunday, August 19, 2018

Application Events : Way to Communicate Between Different Lightning Components

Application events are used to communicate between different lightning components. These are different from component events as these are not dependent on containment hierarchy.

Application events behaves similar to standard events. There is no need to register for application event by any component. Any component can fire application event where as in component events, components have to register first in order to fire that event.

If you want to learn more about component events, then refer below URL:
Component Events: Way to Communicate Between Components in Containment Hierarchy

Through this blog, I will explain different aspect of using application events:

I have created a lightning app which contains 2 separate components. User is entering some information and sending it to second component by clicking on button.



Below are different steps you need to follow in order to implement application events:

  • Create lightning event "SK_MessagerEvent" of type Application
       <aura:event type="APPLICATION" description="Event template" >    
            <aura:attribute name="msg" type="String" access="GLOBAL"/>                           
       </aura:event>

  • Use below syntax to fire event from lightning component:
        var appEvent = $A.get("e.c:SK_MessengerEvent");
        appEvent.setParams({"msg":component.get("v.ltngUserInput")}); 
        appEvent.fire();

  • Create handler in component which want to consume or catch the information passed using application events.
       <aura:handler action="{!c.handleNotification}" event="c:SK_MessengerEvent" />
  • Create function in controller.js which will get invoked whenever this application event will be fired.
        var sentMessage= event.getParam("msg"); 
        console.log('******sentMessage by app event:'+sentMessage);
        component.set("v.recMsg",sentMessage);

Below is complete code for your reference related to snapshot shown above:


Hope this help!!


More Blogs>>: 
COMPONENT EVENTS: Communicate Between Components in Containment Hierarchy    
INHERITANCE IN LIGHTNING    
FIRING EVENT FROM LIGHTNING COMPONENT AND PASSING IT TO VF PAGE    
CHANGES TO LIGHTNING DATA SERVICE IN SUMMER'17    
LIGHTNING DATA SERVICES    
PASSING LIGHTNING COMPONENT ATTRIBUTE VALUE FROM VF PAGE    
FIRE LIGHTNING EVENTS FROM VF PAGE    
DYNAMICALLY CREATING AND DESTROYING LIGHTNING COMPONENTS    
RAISING AND HANDLING CUSTOM EVENTS IN SALESFORCE LIGHTNING    
WHY TO USE DESIGN RESOURCE AND HOW TO ADD DYNAMIC OPTION TO DATASOURCE    
PASSING LIGHTNING COMPONENT ATTRIBUTE VALUE FROM VF PAGE    
PASSING INNER WRAPPER CLASS TO LIGHTNING COMPONENT    
LIGHTNING COMPONENT FOR RECORDTYPE SELECTION FOR ANY SOBJECT    
CUSTOM COMPONENT TO SHOW/HIDE SPINNER IMAGE    

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.

If you want to learn more about Application events, then refer below URL:
Application Events : Way to Communicate Between Different Lightning Components

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"/>
        </aura:event>

  • 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"});                               
        newEvent.fire();
  • 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" 
                  action="{!c.handleNotification}"/>
  • 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:

SessionIdUtility.checkIfUserSessionIdExistInFutureCall();



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:

MetadataService.cls

Below is sample to code to create remote site settings.


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

 MetadataAPIUtility.createRemoteSiteSettings();

Below is snapshot of remote site settings created


Note:
  • 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