Posts

Showing posts from 2018

Salesforce Apex: If else statements vs Switch statements

In this article, we will compare the performance of If else statements vs the Switch statements available in Apex. Switch statements was a long awaited feature in Apex and Salesforce released the Switch statements in the Summer '18 release. This article will help the developers to decide whether to use the traditional If else statements or to go with the newly introduced Switch statement. I have done a simple test to compare the performance of both. The test involves querying the Account records, along with an inner query to fetch the related contacts and then iterating over them in a loop. The first test uses the If else statements and the second test uses the Switch statement. Performance of If else statement: List<Account> accList = [SELECT Name,(SELECT Name FROM Contacts) FROM Account]; System.debug('Start Time: '+Limits.getCpuTime()); Integer i = 0; for(Account acc: accList){     if(i == 0){         System.debug('Account List: '+accList);    

Salesforce Lightning: Global Search

Image
Today we will have a look at creating a custom global search using Lightning and SOSL. We will use SOSL to query the results from database and show it on the lightning component using the lightning:datatable tag SOSL is preferred over SOQL in use cases where we want to search for a particular keyword across different objects. The return type of SOSL will be a list of list of sobjects.  List<List<sObject>> searchResult = [FIND :searchKey                                             IN ALL FIELDS RETURNING                                             Account (Id, Name, AccountNumber, Website),                                             Contact(Id, Name, Email, MobilePhone),                                             Opportunity(Id, Name, StageName, CloseDate),                                             Lead(Id, Name, Email, Company)]; Lightning datatable was introduced by Salesforce in Winter 18 release. With lightning datatable, now it has become very easy to

Salesforce Lightning: Countdown timer

Image
Today we will have a look at how to create a lightning component for countdown timer which can be used on pages where an operation needs to be completed within a specific time period. Example, an online exam or a banking transaction which expires after a specific time period. The component contains a datetime field which represents the end time. Once the user selects the date and time and clicks on Start Timer button, the countdown timer will start. Below is the lightning component: CountdownTimer.cmp <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >       <aura:attribute name="endTime" type="String"/>       <aura:attribute name="timeLeft" type="String"/>       <lightning:tile label="Time Left" class="slds-page-header">         {!v.timeLeft}     </lightning:tile>

Salesforce Lightning: Password Strength Meter

Image
Today we will have a look at how to create a lightning component which analyses the strength of your password. The component only uses the standard lightning tags and no external JS library has been used to achieve this functionality. The functionality is handled completely at the client with no apex code, which makes it performance centric. To allow the user to enter password, I have used lightning:input with type="password". There is an ochange event configured on this input element which makes a call to JS controller and analyses the password. Once the analysis is completed, it sets a score for the entered password. To display the strength of the password, I have used below 2 components: lightning:slider lightning:progressBar Both the components use the score which is set by the controller method. If you want to add some chart which displays the strength, you can use JS libraries like chart.js. PasswordStrengthMeter.cmp <aura:component implements=&qu

Winter 19: Top Features

Image
Winter 19 has a lot of new features released which provides more flexibility while creating apps in Salesforce. Here are some of my favourite features: New component lightning:map Before Winter 19 release, to display a map, you need to use external JS library like Leaflet. But with Winter 19, maps will be supported by the platform using lightning:map. This tag is similar to the apex:map tag available in Visualforce page. New attribute in lightning:accordion Before Winter 19 release, the default behaviour of accordion was that only one section can be opened at a time. If you want to open all the sections at once, it was not supported. The workaround was to use html accordion instead of lightning accrodion. But now with Winter 19, Salesforce has introduced a new attribute called allowMultipleSectionsOpen. If this attribute is set to true, then all the sections can be opened at the same time. Mark Apex Method as Cacheable As mentioned in my last blog, the current solution to

Salesforce Lightning: Client side caching using Storable action

Image
Worried about the performance of your lightning components? Frequent calls to server are degrading the performance of your lightning components? If yes, then the answer to all such problems is Storable actions. Storable actions in lightning are a way to cache your data at the client side. This is all internally managed by the platform and minimal custom code is required for this. To make an action storable, you simply call its setStorable() function. For example: var action = component.get("c.getPicklistOptions"); action.setStorable(); action.setCallback(this, function(response) {     // handle response }; $A.enqueueAction(action); How does Storable action works? If you call the setStorable method before calling the server side method, then the platform compares the previous server call with the current server call. If the parameters to both the server calls are same, then it skips the server call and instead returns the response from the client side cache. Th

Salesforce Lightning: Dependent Picklist - State Country picklist

Dependent picklist are not supported by default in the lightning components. There is no component provided by Salesforce through which we can have dependent picklist in custom lightning components. Fot this example, I have created two custom fields of type picklist - Country and State on Account. After creating the fields, I have setup dependency between them where Country is the controlling picklist and State is the dependent picklist. Below are the components used: Picklist.cmp - Generic component to display picklist in lightning component. Just pass in the object api name and field api name to display the picklist. StateCountryPicklist.cmp - This component displays the state values as the per controlling country value passed to it. Below is the code for all the components: StateCountryPicklist.cmp <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global&quo