Thursday, April 25, 2019

how to use lightning web component(lwc) in lightning communities

This post explains how to use lighting web components in communities.

To use the lightning component in communities component must implement the forceCommunity:availableForAllPageTypes interface but in lightning web components we will use the Configuration file.
Ex: <componentname>.js-meta.xml
Config the Configuration file as per the requirement.

Example:

lwcCommunitiesDemo.html
<template>
    <lightning-card title="Design Attribute Demo" icon-name="custom:custom19">
        <center>
            <h3>HI, <b>{strName}</b></h3><br />
            <div if:true={showDetails}>
                <ul>
                    <li> Emp Name: <b>{empName}</b></li>
                    <li> Emp Department: <b>{empDepartment}</b></li>
                    <li> Emp Location: <b>{empLocation}</b></li>
                    <li> Emp Age: <b>{empAge}</b><br /></b></li>
                    <li> Emp Gender: <b>{empGender}</b></li>
                </ul>
            </div>
        </center>
    </lightning-card>
</template>

lwcCommunitiesDemo.js
export default class lwcCommunitiesDemo extends LightningElement {
    @api showDetails = false;
    @api strName;
    @api empName;
    @api empDepartment;
    @api empLocation;
    @api empAge;
    @api empGender;
}

<target>
1.  This property indicates that where we need to use the component(record page, app page, and home page, etc).
2.  To use component in communities add lightningCommunity__Page, lightningCommunity__Default targets in Configuration file.
<targetConfigs> 
These properties are used to design your component as per your need.

lwcCommunitiesDemo.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="lwcCommunitiesDemo">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
      <targets>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
    </targets>

      <targetConfigs>
        <targetConfig targets="lightningCommunity__Default">
            <property name="strName" type="String" default="Salesforce Code Crack" label="Enter the Employee Name"/>
            <property name="showDetails" type="Boolean" default="true" label="Do you want to Show Details ?"/>
            <property name="empName" type="String" default="" label="Enter Employee Name"/>
            <property name="empDepartment" type="String" default="" label="Enter Employee Department"/>
            <property name="empLocation" type="String" label="Enter Employee Location" datasource="Hyderabad, Delhi, Pune, Noida"/>
            <property name="empAge" type="integer" label="Enter Employee Age"/>
            <property name="empGender" type="String" label="Enter Employee Gender" datasource="Male, Female"/>
        </targetConfig>
      </targetConfigs>

</LightningComponentBundle>

Result

Tuesday, April 23, 2019

How to get all batch apex class names in org?

This post explains how to get all apex batch class names list in Org.

Example:

list<ApexClass> lstApexClasses = [SELECT Id, Name, ApiVersion, Body FROM ApexClass where NamespacePrefix = null];
for(ApexClass iterator : lstApexClasses) {
    if(iterator.Body.contains('Database.Batchable')) {
        system.debug('Batch Class Name ====> '+iterator.Name+' Api Version ====> '+iterator.ApiVersion);
    }
}

Result

Wednesday, April 17, 2019

How to get the batch apex class total run time in minutes using apex

This post explains how to get the batch apex start time, end time and batch apex total run time in minutes.
some times we need to track the running time batch class, this example shows how to track time.

Example:

ChangeAccountIndustryBatch.cls
global class ChangeAccountIndustryBatch implements Database.Batchable<sObject>, Database.stateful {
    // start method
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Id, Name, Industry, Type From Account Where Type = \'Customer - Channel\'');
    }
    // execute method
    global void execute(Database.BatchableContext bc, list<sObject> scope) {
        if(scope != null && !scope.isEmpty()) {
            list<Account> lstAccsToUpdate = new list<Account>();
            for(Account accIterator : (list<Account>)scope) {
                accIterator.Industry = 'Banking';
                lstAccsToUpdate.add(accIterator);
            }
            
            if(!lstAccsToUpdate.isEmpty()) {
                Database.update(lstAccsToUpdate, false);
            }
        }
    }
    // finish method
    global void finish(Database.BatchableContext bc) {
        AsyncApexJob objAsyncJob = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors, ExtendedStatus, CompletedDate, CreatedDate FROM AsyncApexJob WHERE Id = :bc.getJobId()];
     
        String strSubject = 'ChangeAccountIndustryBatch with id ' + objAsyncJob.Id + ' is completed with ' + objAsyncJob.Status;
        
         // Email Body
        String strBatchBody = 'The batch apex job processed ' + objAsyncJob.TotalJobItems + ' batches with ' + objAsyncJob.NumberOfErrors + ' failures.<br/>'
                   + 'Start time: <b>' + objAsyncJob.CreatedDate.format('MM/dd/yyyy HH:mm') + '</b><br/>'
                   + 'End time: <b>' + objAsyncJob.CompletedDate.format('MM/dd/yyyy HH:mm') + '</b><br/>'
                   + 'Time taken in minutes: <b>' + ((Decimal)(objAsyncJob.CompletedDate.getTime() - objAsyncJob.CreatedDate.getTime())/1000/60).setScale(2) + '</b><br/>';
        
        // Single email
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        
        // Sets the paramaters of the email
        email.setSubject(strSubject);
        email.setHtmlBody(strBatchBody);
        email.setTargetObjectId(UserInfo.getUserId());
        email.setSaveAsActivity(false);

        // Sends the email
        list<Messaging.SendEmailResult> lstEmailResult = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        
    
    }
}


Run script from Annoymous window
Database.executeBatch(new ChangeAccountIndustryBatch());

Result

Tuesday, April 2, 2019

How to design attributes in Lightning Web Components(lwc)

This Post explains how to design attributes in Lighting Web Components(lwc)

In Lightning Component we use Design Resource to create design attributes,  <design:attribute> tag in the design file,
but for Lighting Web Components we need to define the design attribute in Component Configuration File (XML) 
For more information, about config file check this Link

Each Lightning web component folder must include a configuration file named <component>.js-meta.xml. The configuration file defines the metadata values for the component, including the design configuration for the Lightning App Builder and Community Builder.

Example:

designComponentLWCDemo.html
<template>
    <lightning-card title="Design Attribute Demo" icon-name="custom:custom19">
        <center>
            <h3>HI, <b>{strName}</b></h3><br />
            <div if:true={showDetails}>
                <ul>
                    <li> Emp Name: <b>{empName}</b></li>
                    <li> Emp Department: <b>{empDepartment}</b></li>
                    <li> Emp Location: <b>{empLocation}</b></li>
                    <li> Emp Age: <b>{empAge}</b><br /></b></li>
                    <li> Emp Gender: <b>{empGender}</b></li>
                </ul>
            </div>
        </center>
    </lightning-card>
</template>

designComponentLWCDemo.js
import { LightningElement, api} from 'lwc';

export default class DesignComponentLWCDemo extends LightningElement {
    // tracking attributes values
    @api showDetails = false;
    @api strName;
    @api empName;
    @api empDepartment;
    @api empLocation;
    @api empAge;
    @api empGender;
}

designComponentLWCDemo.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="designComponentLWCDemo">
    <apiVersion>45.0</apiVersion>
    <isExposed>true</isExposed>
      <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
     <!-- Configuring the design attributes -->
      <targetConfigs>
        <targetConfig targets="lightning__HomePage,lightning__RecordPage">
            <property name="strName" type="String" default="Salesforce Code Crack" label="Enter the Employee Name"/>
            <property name="showDetails" type="Boolean" default="true" label="Do you want to Show Details ?"/>
            <property name="empName" type="String" default="" label="Enter Employee Name"/>
            <property name="empDepartment" type="String" default="" label="Enter Employee Department"/>
            <property name="empLocation" type="String" label="Enter Employee Location" datasource="Hyderabad, Delhi, Pune, Noida"/>
            <property name="empAge" type="integer" label="Enter Employee Age"/>
            <property name="empGender" type="String" label="Enter Employee Gender" datasource="Male, Female"/>
        </targetConfig>
      </targetConfigs>

</LightningComponentBundle>


Demo