Tag Archives: Lightning

Salesforce Lightning URL Hacking

Here I have created a custom detail button(New Contact) on Account object with some pre-populated Contact field values from Account record using Lightning URL Hacking.

Go to setup | Object Manager | Account | Buttons, Links, and Actions | Create a new Button

  • Label – New Contact
  • Display Type – Detail Page Button
  • Behavior – Display in new window
  • Content Source – URL

URL Content:

/lightning/o/Contact/new?defaultFieldValues= OwnerId={!Account.OwnerId},AccountId={!Account.Id},MailingStreet={!Account.ShippingStreet},MailingCity={!Account.ShippingCity},MailingState={!Account.ShippingState},MailingPostalCode={!Account.ShippingPostalCode},MailingCountry={!Account.ShippingCountry}

Add this button to Account detail page layout, “Mobile & Lightning Action” section.

Note: This URL Hacking with button doesn’t work in Salesforce1 mobile app.

Lightning Custom Datatable With Pagination

Apex Class:

public class AccountController {
    @AuraEnabled//Get Account Records
    public static List<Account> getAccountList(Integer pageSize, Integer pageNumber){
        List<Account> accList = new List<Account>();
        ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(Database.getQueryLocator('SELECT Id, Name, AccountNumber, Industry, Phone FROM Account'));
        accList = (List<Account>)ssc.getRecords();
        return accList;

Lightning Component:

<aura:component implements="flexipage:availableForAllPageTypes,force:appHostable" controller="AccountController">
    <!--Declare Attributes-->
    <aura:attribute name="accList" type="Account[]"/>
    <aura:attribute name="pageNumber" type="Integer" default="1"/>
    <aura:attribute name="pageSize" type="Integer" default="10"/>
    <aura:attribute name="isLastPage" type="Boolean" default="false"/>
    <aura:attribute name="dataSize" type="Integer" default="0"/>
    <!--Declare Handler-->
    <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    <!--Component Start-->
    <div class="slds-m-around--xxx-large">               
            <aura:set attribute="title">
            <aura:set attribute="footer">
                <div class="slds-align_absolute-center"> 
                    <div class="slds-p-right_xx-small">
                        <lightning:button label="Prev"
                                          disabled="{! v.pageNumber == 1}"
                    <span class="slds-badge slds-badge_lightest">
                        Page {!v.pageNumber} | Showing records from {! ((v.pageNumber-1)*v.pageSize)+' to '+((v.pageNumber-1)*v.pageSize+v.dataSize)}
                    <div class="slds-p-left_xx-small">
                        <lightning:button label="Next"
            <table class="slds-table slds-table_cell-buffer slds-table_bordered">
                    <tr class="slds-line-height_reset slds-text-title_caps">
                        <th  class="slds-is-resizable" scope="col">
                            <div class="slds-truncate" title="Name">
                        <th  class="slds-is-resizable" scope="col">
                            <div class="slds-truncate" title="Account Number">
                                Account Number
                        <th class="slds-is-resizable" scope="col">
                            <div class="slds-truncate" title="Industry">
                        <th class="slds-is-resizable" scope="col">
                            <div class="slds-truncate" title="Phone">
                        <th scope="col">
                            <div class="slds-truncate" title="Actions">Actions</div>
                <aura:if isTrue="{!v.accList.length > 0}">
                        <aura:iteration items="{!v.accList}" var="acc">
                            <tr class="slds-hint-parent">
                                <th data-label="Name" scope="row">
                                    <div class="slds-truncate" title="{!acc.Name}">
                                <td data-label="Account Number">
                                    <div class="slds-truncate" title="{!acc.AccountNumber}">{!acc.AccountNumber}</div>
                                <td data-label="Industry">
                                    <div class="slds-truncate" title="{!acc.Industry}">{!acc.Industry}</div>
                                <td data-label="Phone">
                                    <div class="slds-truncate" title="{!acc.Phone}">{!acc.Phone}</div>
                                <td data-label="Action">
                                    <lightning:buttonIcon name="{!acc.Id}" iconName="utility:delete" title="Delete" variant="bare" onclick="{!c.handleDeleteAccount}" alternativeText="Delete" />                                                                            
            <aura:if isTrue="{!empty(v.accList)}">
                <div class="slds-align_absolute-center">
                    No records found
    <!--Component End-->

Lightning JS Controller :

    doInit : function(component, event, helper) {        
        helper.getAccounts(component, event);
    handleNext : function(component, event, helper) { 
        var pageNumber = component.get("v.pageNumber");
        component.set("v.pageNumber", pageNumber+1);
        helper.getAccounts(component, helper);
    handlePrev : function(component, event, helper) {        
        var pageNumber = component.get("v.pageNumber");
        component.set("v.pageNumber", pageNumber-1);
        helper.getAccounts(component, helper);
    handleDeleteAccount: function (component, event, helper) {
        alert('Selected Account to delete - ' + event.getSource().get("v.name"));

Lightning JS Helper:

    getAccounts : function(component, event) {
        var action = component.get("c.getAccountList");
            'pageSize' : component.get("v.pageSize"),
            'pageNumber' : component.get("v.pageNumber")
        action.setCallback(this,function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var result = response.getReturnValue();
                if(result.length < component.get("v.pageSize") || result.length == 0){
                    component.set("v.isLastPage", true);
                } else{
                    component.set("v.isLastPage", false);
                component.set("v.dataSize", result.length);
                component.set("v.accList", result);

Return Custom Error Messages From Apex Controller In Salesforce Lightning

Sample Code:

We can use System.AuraHandledException to return custom error message from server side apex controller to lightning component in Salesforce.

string errorMessage = 'Your Error Message';
AuraHandledException auraEx = new AuraHandledException(errorMessage);
throw auraEx;

Count Unique Row Values in Salesforce Lightning Report

In Winter ’20 release Salesforce has introduced report Count Unique Row Values in Lightning experience.

Go to Setup | Reports and Dashboards Settings | Select Enable Unique Row Count Aggregate in Reports (Lightning Experience Only) | click Save.

While editing a report in the report builder, find the column for which you want to count unique values | Click Show More | Show Unique Count | Click Save.