Adding Merge Duplicate Accounts tool in Lightning Experience like in classic.

Salesforce added Duplicate Rules and Lightning Experience is missing Merge Accounts and Contacts in tools. If you would like to the merge tool just like in classic. You can add custom button to list views. Here is how you can do it:

  1. Create a custom button. Display Type – List Button, Content Source: URL and use this URLs :

/merge/accmergewizard.jsp?retURL=/001

2. Add button to the List View Search Layout.

To add button navigate to Object Manager => Account => Search Layouts for Salesforce Classic.

Edit List View layout and add Merge Accounts button.

Now if you you go to any Account List view you will see Merge Accounts button.

Adding users to permission set through Apex

Here is how we can add/remove a user to/from a permission set through Apex. My requirement was to add or remove a user from a permission set when the value of a checkbox changes in user object.So I have this code in User Update After trigger.

Here is the code to add to a PermissionSet:

Here is the code to remove from a PermissionSet:

 

Lightning Web Component in Utility Bar example

Here I will show how to make lightning web component available to be used as Utility Bar Item.

First create new Lightning Web Component.

sfdx force:lightning:component:create --type lwc --componentname UtilityBarExample --outputdir force-app\main\default\lwc

Open meta configuration XML file utilityBarExample.js-meta.xml and change it – add targets lightning__UtilityBar and lightning__AppPage and change
isExposed to true, so the file looks like this:

Create Scratch org if you don’t have one (quick command that uses default config and 30 duration day for Scratch org):

sfdx force:org:create -f config\project-scratch-def.json --setalias LightningWebComponents --durationdays 30 --setdefaultusername --json --loglevel fatal

Push code to your scratch org:

sfdx force:source:push --json --loglevel fatal

Go to Setup –> Apps –> App Manager.

Select the app that you want to add Utility Item to and select Edit. Then go to Utility Items and add Utility Item.


You should be able to see your custom component in the list of available components.Now you can add this Lightning Web Component in Utility Bar.

Lightning Web Component Datatable example with sorting and search functionality.

Many people are familiar with lightning datatable component and how to use it in Aura Component. In this article I want to show example how to use lightning component in Lightning Web Component and build simple search by name and column sorting.

First we need to create an apex class.

You can create it either in Salesforce UI or Developer Console or do it in Visual Studio Code with SFDX command:

sfdx force:apex:class:create --classname GetAccounts --template DefaultApexClass --outputdir force-app\main\default\classes

Code for Apex class:

Second we will need to create Lightning Web Component budle.

sfdx force:lightning:component:create --type lwc -n LightningDatatableExample -d force-app/main/default/lwc

Code for html component:

JS controller:

To make sure component is visible in Lightning App Builder you need to add targets and change isExposed to true in LightningDatatableExample.js-meta.xml file:

How to clear selected rows in Lightning DataTable?

I have a lightning datatable where I am displaying leads.

Once I selected the rows that I wanted and completed the action, I noticed that the checkboxes are not getting unchecked.

Here is what I added to the action method to make sure that the checkboxes will be unchecked after the action is completed.

Hope it helps 🙂

How to create Popup Using Lightning Web Components?

Lightning Web Components is the new programming model that you can use to build Lightning components in Salesforce. Here Is how you can create a Popup in Salesforce using LWC.

The following is the JavaScript that foes in the filename.js

import { LightningElement , track } from 'lwc';

export default class PopUpExample extends LightningElement {
    @track openModal;
    openPopUp() {
        this.openModal = true;
    }
    closePopUp() {
        this.openModal = false;
    }
    saveAction(){
        // eslint-disable-next-line no-console
        console.log( 'saveaction is called');
        this.closePopUp();
    }
}

Here is the xml file

When you create a new lwc, make sure the isExposed is updated to True in the xml file. The default is false, the component will not show up in the Lightning Builder even if you deploy the component to your org. Also update the <targets> based on what your requirements are.

That’s it.. Hope it helps.

How to use static resource to style Lightning Web Components?

Here is a sample code on how to import and use static resources for styling your lwc.

I zipped my CSS file into folder and uploaded this into a static resource called ‘myResource’. Here is the JS code you would need to import this static Resource into your component.

import { LightningElement, track } from 'lwc';
import { loadStyle } from 'lightning/platformResourceLoader';
import myResource from '@salesforce/resourceUrl/myResource';
export default class CssExample extends LightningElement {
    @track textVal;
    changeHandler(event) {
        this.textVal = event.target.value;
    }
    renderedCallback() {
        Promise.all([
            loadStyle(this, myResource + '/styleSheet.css')
        ]).catch(error => {
             // eslint-disable-next-line no-console
             console.log(error);
            
        });
    }
}

Here is how you can use a style class that you have defined inside the static resource:

That’s it. Hope it helps 🙂

Lightning component table using Lightning Datatable with sorting and pagination.

I recently worked on the project where I needed to create table with pagination, sorting and be able to select page size.

I tried to find a good working example online and could find example where it will work with unlimited number of pages. I saw examples that used LIMIT in the query, which mean not all the records will be shown in the table.

I decided to write a component that will work with any number of records and will use StandardSetController and it’s methods like next(), previous() etc.

LightningTable.cmp

LightningTableController.js

LightningTableHelper.js

LightningTableController.apxc

 public class LightningTableController {
    private static ApexPages.StandardSetController ssc;
    private static String query = 'SELECT Id, Name, FirstName, LastName, Email, MobilePhone FROM Contact';
    
    @AuraEnabled
    public static ResultWrapper getData( Integer pageSize, Integer pageNumber, String sortBy, String sortOrder ){
        
        if( sortby != NULL &amp;&amp; sortOrder != NULL  ){
            query += ' ORDER BY '+ sortby+' '+ sortOrder+' NULLS LAST';
        }
       
        ssc = new ApexPages.StandardSetController( Database.getQueryLocator( query ) );
        //set page size
        ssc.setpagesize(pageSize);
        //set page number 
        ssc.setPageNumber( pageNumber );
        return new ResultWrapper( (List)ssc.getRecords(), ssc.getResultSize());
    } 
    
    public class ResultWrapper {
        
        @AuraEnabled
        public List result {get;set;}
        @AuraEnabled
        public Integer totalNumberOfRecords {get;set;}
        
        public ResultWrapper( List result, Integer totalNumberOfRecords) {
            this.result = result;
            this.totalNumberOfRecords = totalNumberOfRecords;
        }
    }
}

} 

		
					
			

How to fix error “Java runtime could not be located. Set one using the salesforcedx-vscode-apex.java.home VS Code setting” in Visual Studio code.

Today I was trying to set up Visual Studio Code on my personal computer and when I tried to create project I received this error: “Java runtime could not be located. Set one using the salesforcedx-vscode-apex.java.home VS Code setting”.

The message is self-explanatory, however it took me few minutes to figure out how to fix it.

What is causing issue?

Current version of Salesforce Extension Pack for Visual Studio Code requires Java Platform, Standard Edition Development Kit 8 (JDK8). If you don’t have it – install it first, you can find it and download it here.

I received this error even though I had JDK8 installed and configured in PATH environment variable to point to the right folder.

How to fix this error?

In Visual Studio Code go to File –> Preferences –> Settings or (Ctrl + comma)

Scroll down and find Salesforce Apex Configuration and on the right side add:

“salesforcedx-vscode-apex.java.home”: “path_to_JDK8”