Apex Less Lightning Component

06:10 0 Comments A+ a-


APEX Less Lightning Component




Hi all, as we know Salesforce is providing a lot of feature for lightning developers. I am going to discuss some of them with you.

Display Record Data:
Yes, now we can create a lightning component to view the data of a record without writing a  single line of a controller (Apex And JS Both).

Lightning Provide us a Component name lightning:recordViewForm.  This component requires API Version 41.0 and later.
This component takes recordId and done rest of things internally. It also takes care of Field-Level-Security and Sharing for you. So user see data only they have access.

Here is the code snippet
<aura:component implements="force:hasRecordId,lightning:actionOverride"> <lightning:card variant="brand" title="Contact" iconName="standard:contact"> <p class="slds-p-horizontal_small"> <lightning:recordViewForm recordId="{!v.recordId}" objectApiName="Contact"> <div class="slds-grid"> <div class="slds-col slds-size_1-of-2"> <lightning:outputField fieldName="Name"></lightning:outputField> </div> <div class="slds-col slds-size_1-of-2"> <lightning:outputField fieldName="Email"></lightning:outputField> </div> </div> <div class="slds-grid"> <div class="slds-col slds-size_1-of-2"> Account <lightning:outputField fieldName="AccountId" variant="label-hidden"></lightning:outputField> </div> <div class="slds-col slds-size_1-of-2"> <lightning:outputField fieldName="Phone" ></lightning:outputField> </div> </div> <div class="slds-grid"> <div class="slds-col slds-size_1-of-2"> <lightning:outputField fieldName="Birthdate"></lightning:outputField> </div> <div class="slds-col slds-size_1-of-2"> <lightning:outputField fieldName="MailingAddress"></lightning:outputField> </div> </div> </lightning:recordViewForm> </p> </lightning:card> </aura:component>

Let’s Have a look at the code we have written.

force:hasRecordId:  This interface helps us to read the id from the browser’s URL. Internally it adds an aura:attribute into over component which name is recordId.

lightning:actionOverride: It makes our component visible into override window.



lightning: recordViewForm: This is wrapper component that accepts recordId and objectApiName and displays one or more field value with the help of lightning:outputField. recordId and objectApiName are required.



lightning:outputField: It accepts the fieldName and display the field value in the correct format according to data type. We need to wrap this inside the lightning:recordViewForm tag. User’s locale setting helps to display format for number, date etc.


Consideration before using lightning:outputField:  Lookup values are rendered as text. Linking and hover overlays on the field value are not supported till now.



Edit a record or Create new Record:  Yes we can edit and create a record without writing a single line of the apex. We can use lightning:recordEditForm tag.

This tag is very much similar to lightning:recordViewForm tag. As it accepts recordId and objectApiName to work. And also manage Field-Level-Security and sharing.

It has some cool feature regarding save a record or update its value. If a lightning:button component with type=”submit” is used inside the lightning:recordEditForm then it automatically saves the changes to salesforce.



If we provide value for recordId then this component works for edit that record(Override component on the edit button of Object). But if recordId is not available then it acts for creating a new record(override component for the new button of the object). It automatically handle the errors in saving record, we must include lightning:message component inside the lightning:recordEditForm.

It also accepts recordTypeId, So if we have multiple recordTypes and do not have a default one, we can pass record Type Id for the form.

Some other attribute for lightning:recordEditForm.

onerror: The action triggered when there is an error on form submission.
onload: The action triggered when the form data is loaded.
onsubmit: The action triggered when the form is submitted.
onsuccess: The action triggered when the form is saved.

onerror action returns the ‘error’ parameter.  It contains the following properties.
errorCode
message

onsuccess action returns the ‘response’ parameter. It contains the following properties.
apiName: API name of the object. Such as Contact, Account.
childRelationships: The child relationship data for this record.
fields: The field data for this record, matching the requested layout.
id: The ID of this record.
recordTypeInfo: The record type info for this record, if any.



Code Snippets:
Lightning Component:
<aura:component implements="force:hasRecordId,lightning:actionOverride"> <lightning:card title="Contact" iconName="standard:contact" class="slds-p-around_medium"> <lightning:recordEditForm recordId="{!v.recordId}" objectApiName="Contact" onsuccess="{!c.recordSaved}"> <lightning:messages /> <div class="slds-grid"> <div class="slds-col slds-size_1-of-2 slds-p-around_medium"> <lightning:inputField fieldName="FirstName"></lightning:inputField> </div> <div class="slds-col slds-size_1-of-2 slds-p-around_medium"> <lightning:inputField fieldName="LastName"></lightning:inputField> </div> </div> <div class="slds-grid"> <div class="slds-col slds-size_1-of-2 slds-p-around_medium"> <lightning:inputField fieldName="Email"></lightning:inputField> </div> <div class="slds-col slds-size_1-of-2 slds-p-around_medium"> <lightning:inputField fieldName="Phone"></lightning:inputField> </div> </div> <div class="slds-grid"> <div class="slds-col slds-size_1-of-2 slds-p-around_medium"> <lightning:inputField fieldName="AccountId"></lightning:inputField> </div> </div> <lightning:button type="submit" label="Save" variant="brand"></lightning:button> </lightning:recordEditForm> </lightning:card> </aura:component>


Controller(JS):
({ recordSaved : function(component, event, helper) { var params = event.getParams(); //alert('record Is Saved with id '+params.response.id); console.log(params.response.childRelationships); console.log(params.response.fields); console.log(params.response.recordTypeInfo); console.log(params.response.id); } })
Thanks,




Schedule multiple batch classes using single Schedule class

05:00 0 Comments A+ a-

Schedule multiple batch classes using single Schedule class

Hi all,
Today we are going to discuss how we can schedule different batch classes for different time using single Schedule class. Till now I was creating new schedule class for each of my batch class.

GlobalScheduler:

/* Name : GlobalScheduler Date : 30 march 2018 Author : Arpit vijayvergiya Description : We are creating this class as global scheduler. So multiple batchclass can be scheduled with single scheduler. */ public class GlobalScheduler implements Schedulable{ public Database.Batchable<sObject> batchToBeSchedule; public Integer batchSize; public GlobalScheduler(Database.Batchable<sObject> batchToBeSchedule , Integer batchSize){ this.batchToBeSchedule = batchToBeSchedule; this.batchSize = batchSize; } public void execute(SchedulableContext sc){ Database.executeBatch(batchToBeSchedule,batchSize); } }

AccountBatch:

/* Name : AccountBatch Date : 30 march 2018 Author : Arpit vijayvergiya Description : We create this batch class to test GlobalScheduler class. */ global class AccountBatch implements Database.Batchable<sObject>{ global Database.QueryLocator start(Database.BatchableContext dbc){ return Database.getQueryLocator([SELECT Id,Name FROM Account LIMIt 10]); } global void execute(Database.BatchableContext dbc, List<SObject> listOfAccount) { system.debug('Account'+ listOfAccount); } global void finish(Database.BatchableContext dbc){ System.debug('Account batch finish'); } }


ContactBatch:

/* Name : ContactBatch Date : 30 march 2018 Author : Arpit vijayvergiya Description : We create this batch class to test GlobalScheduler class. */ global class ContactBatch implements Database.Batchable<sObject>{ global Database.QueryLocator start(Database.BatchableContext dbc){ return Database.getQueryLocator([SELECT Id,lastName FROM Contact LIMIt 10]); } global void execute(Database.BatchableContext dbc, List<SObject> listOfContact) { system.debug('Contact '+ listOfContact); } global void finish(Database.BatchableContext dbc){ System.debug('contact batch finish'); } }
How to schedule: We can not schedule this class using standard Salesforce UI. This is the only disadvantage I found till now. We need to schedule it using Apex code. We need to make a proper cron expression for that. There are some online tools available for creating cron expression. ie. CronMaker.

If you scheduled a batch class using this scheduler class and then again you need to schedule another batch class at the same time or different time. It will not affect any of our scheduled jobs.

Here is the snippets for scheduling single scheduler multiple time with multiple batches.




AccountBatch accBatch = new AccountBatch(); // Batch Class Name GlobalScheduler scheduler = new GlobalScheduler(accBatch,100); String sch = 00 + ' ' + 57 + ' ' + 15 + ' * * ?'; // this cron schedule batch class to be run at 3 pm 57 minute on daily basis System.schedule('Account Batch Scheduler', sch, scheduler); ContactBatch conBatch = new ContactBatch(); // Batch Class Name GlobalScheduler scheduler1 = new GlobalScheduler(conBatch,200); String sch1 = 00 + ' ' + 56 + ' ' + 15 + ' * * ?'; // this cron schedule batch class to be run at 3 pm 56 minute on daily basis System.schedule('Contact Batch Scheduler', sch1, scheduler1);

Thanks,