Posts

Using JavaScript Promises in Lightning Components

Image
With JavaScript being a single threaded process by nature, using "JavaScript Promises" it makes JavaScript to be a multithreaded or in other words it is a process of making Asynchronous calls using JavaScript.

JavaScript Promises is a native JavaScript functionality that can be used in Lightning Components. Salesforce recommends using ECMAScript 6 (ES6, often referred to as “Harmony”) which is a standard script language. In case if the browser doesn’t provide a native version, the framework uses a polyfill so that promises work in all browsers supported for Lightning Experience.

Basic Syntax for Promises:

var promise = return new Promise(function(resolve, reject) { setTimeout(resolve, 100, 'foo'); });
Promise can return either resolve (If the result is success) and reject (If the result is fail). Optionally there is one more status called pending. Now let's go ahead and build a simple Lightning Component that will output some user information like current dev…

Using jQuery in Lightning Components to Implement Dragging/Sorting

Image
jQuery is a lightweight JavaScript library, which makes it easy to use JavaScript. Using jQuery we can build some rich UI in our Lightning Components. In order to use jQuery in Lightning Components, we first need to upload jQuery related files to Static Resource. Below are the links for downloading jQuery/jQuery UI
jQuery Download LinkjQuery UI Download Link
Once we have the files downloaded, we upload them to Static Resource using Setup-->Custom Code-->Lightning Components-->Static Resource



Once we have the files uploaded to the Static Resource, we can use ltng:require tag to refer those jQuery files in our Lightning Components at the time of component loading. Below I have a sample component bundle (Component/Controller/Style) to display couple of "div" sections to display an Image that can be dragged to a droppable area and another section to display few rows that can be sorted using jQuery function.

jQueryComp.cmp:This is the component code to display sectio…

Lightning Component for Custom Lightning Templates

Image
Lightning App Builder provides flexibility to create custom Lightning pages which can be used in opposite of standard Lightning pages. Even though every standard Lightning page is associated with a default template component, Custom Lightning page template components are useful when you want a customized template for your business needs.

Custom Lightning pages can be created for.....
App Page - When you want to create separate pages for each app.Home Page - When you want to create separate pages to override the standard home page.Record Page - When you want to create pages on record level for different objects.
In order to create custom Lightning Templates for App/Home/Record pages, it is required to implement below interface for each.
lightning:appHomeTemplatelightning:homeTemplatelightning:recordHomeTemplate
Note** Each template component should implement only ONE template interface. Template components shouldn’t implement any other type of interface, such as flexipage:availa…

Update Custom Metadata Types using Metadata Api

Image
With the introduction of Custom Metadata Types, the use of Custom Settings have been marginally reduced. Custom Metadata Types gives more flexibility in context of creating/maintaining Metadata, however when it comes to the point of updating Custom Metadata Types it involves some manual steps to be done from the Admin side which is a bit pain!

With the help of Metadata Api this process can be made more simple just by writing some Apex. Lets go ahead and put some code to make use of Metadata Api and update Custom Metadata Types. Step 1: Create a new Custom Metadata Type "US_States" with one custom field "State__c".



Step 2: Create few records for the above Custom Metadata Type using "Manage" button.



Step 3: When we say that we update Custom Metadata Types using Metadata Api, behind the scenes it does an actual deployment and we use below class to track those deployment results. Create an Apex class "CustomMetadataCallback.apxc" using below cod…

REST Service to Expose Attachments as Public CURL

Image
My use case behind this blog post is to expose Salesforce Attachment (Image/Pdf/Video) as a public accessible CURL, without having to authenticate to Salesforce. And we are going to use REST Web-services and Public Sites to do this!
Step 1: Create an Apex REST class with below code snippet.
/**Purpose:Apexrestwebservicetoreturnattachmentbodyforagivenid**Developer:SFDC_Dev**/@RestResource(urlMapping='/Document_V1/*')globalclassDocumentV1{@HttpGetglobalstaticvoiddocBody(){RestRequestrequest=RestContext.request;RestResponseres=RestContext.response;try{//RetrieveattachmentidfromtherequesturlStringdocId=request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);//QueryattachmentsObjecttogetthespecificattachmentusingattachmentidAttachmenta=[SelectId,Name,BodyFromAttachmentWhereId=:docId];//Assingproperimageextensionbaseonfiletypeif(a.Name.containsIgnoreCase('.jpeg')||a.Name.containsIgnoreCase('.jpg'))res.addHeader('Content-Type','image/jpg…

Few Apex Scenario Based Programming Questions

Image
Recently at one of the local Hackathon I came across some scenario based Apex programming questions and I thought I would go ahead and blog on them. Below are three scenarios on which we are going to write some Apex code!
Scenario 1: Create a Method which accepts List of String and returns a Map with count on how many times a specific value is repeated in a given List. Example:- List<'A', 'B', 'A', 'C', 'B', 'A'> should result in "{A=3, B=2, C=1}"

Approached Solution: Below is my Apex class with a method to accept List and returns a Map.
/**Purpose:HackathonProgrammingScenarios**Developer:HackathonDev*/publicclassHackathoScenarios{//MethodtoreturnamapwithstringoccurencecountthatwaspassedinalistpublicstaticMap<String,Integer>scenario1(List<String>lstStrng){Map<String,Integer>mapVals=newMap<String,Integer>();for(Strings:lstStrng){if(mapVals.containsKey(s)){mapVals.put(s,mapVals.get(s)+1);}else{mapVals…

Lightning RecordForm - An enhanced Lightning Data Service

Image
As always with every release Salesforce introduces bunch of new base components and this time they have "Lightning: recordForm" which suppresses using "lightning:recordEditForm and lightning:recordViewForm" separately to handle record view and edit.
Lets go ahead and put some sample code to create a Lightning Component using lightning recordForm!

RecordFormComp.cmp: <aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasSObjectName,force:hasRecordId,force:lightningQuickAction" access="global" > <aura:attribute name="fieldsList"type="String[]" default="['Id', 'Name', 'BillingAddress', 'AnnualRevenue']"/> <div> <center> <h1><b>Lightning Record Form<br/> Object_Name: {!v.sObjectName} - Record_Id: {!v.recordId} </b> …