Thursday, 2 April 2020

Lightning Web Components Naming Convention

In this post we will talk about Lightning Web Components Naming Convention rules. We will also talk about example of PascalCase, camelCase or kebab-case and how those naming convention rules works in LWC.

Components Bundles

Lightning web component bundles include all below files by default. Its also have some Optional component like CSS, SVG Icon.




Lets see what is the naming convention :

Naming Convention / Rules for Components Bundles

  • Component name Must begin with a lowercase letter
  • Name must contain only alphanumeric or underscore characters
    • Can’t contain a hyphen (dash)
  • Must be unique in the namespace (No other Aura or LWC can have this name)
  • Can’t include whitespace
  • Can’t end with an underscore
  • Can’t contain two consecutive underscores
  • Folder and files names must have same “prefix name”.

HTML File


Use camel case to name your component and use kebab-case to reference a component in the markup. For example



JavaScript File


Java Script Class name should be in PascalCase like below example :

CSS File


  • Needs to be created in the component bundle
  • Has the same name as the component
  • Uses standard CSS syntax
  • Unlike Aura, no need for .THIS

What is the different between camelCase, PascalCase and kebab-case ?


camelCase : Each word in the middle of the respective phrase begins with a capital letter. for example apexHours.
PascalCase: It is same like Camel Case where first letter always is capitalized. for example ApexHours. 
kebab-case: Respective phrase will be transferred to all lowercase with hyphen(-) separating words. for example apex-hours.

Case Name camelCase PascalCase kebab-case
Example helloWorld HelloWorld hello-world
Where to use Folder
File Names
Property name in JS
Class in Java Script Component reference in markup
HTML attribure name

 
 
Please check below post on Lightning Web Components:-

Thanks.
Amit Chaudhary

Monday, 23 March 2020

Salesforce Developer Training Free

Want to become a Salesforce Developer ? Here is Free Salesforce Training for beginners. We are glad that we did one step by step Salesforce developer training for beginners and recorded. More then 2400 student registered in this ApexHours Training and learned apex across the world.

Salesforce Developer are one of the best paid resources in the market. But there is no way to become a good Salesforce developer without being a good Salesforce admin. If you are new then check this Salesforce Admin training.

Salesforce Developer Training

We covered the follow topic in that Developer training and good news is that all session are recorded and uploaded on YouTube. Please check below post for Recording, PPT, Trailhead module and further learning details.
  1. Introduction to Apex Part 1
  2. Introduction to Apex Part 2
  3. Tools for Development & Debugging Apex
  4. Querying & Manipulating Salesforce Data
  5. Writing Apex Triggers
  6. Writing Unit Tests in Apex
  7. Asynchronous Processing in Apex
  8. Integrating with Salesforce
  9. Integrating with Salesforce
  10. Building Custom User Interfaces using Visualforce
  11. Custom User Interfaces using AURA
  12. Building Custom User Interfaces using LwC – Part 1
  13. Building Custom User Interfaces using LwC – Part 2

Our Team spend lots of time to create this free course. So Please like the recording and subscribe the ApexHours YouTube Channel.

Day 1: Introduction to Apex


In this session we covered What is apex, Data Type of Apex, Variable in Apex, Loops, Conditional Statements and Collection in Apex. Here is agenda of session. Check this post for PPT and further learning link.
  1. Declarative Vs Programmatic Approach of Development
  2. Datatypes in Apex
  3. Operators in Apex
  4. System.Debug()
  5. Familiarizing to Dev Tools – VS Code, Anonymous Block
  6. Q & A


Day 2: Introduction to Apex Part 2


In this session we covered Introduction to Class, Method, Static keywords and OOPs Concept in Apex. Here is agenda of session. Check this post for PPT and further learning link.
  1. Anatomy of Class
  2. Methods
  3. Static Vs Instance methods
  4. Pass by Value Vs Reference
  5. Introduction to Object Oriented Programming (OOP)
    1. Extending a Class
    2. Interfaces

Day 3: Demystifying Developer Tools


In this session we covered Developer tools available for Salesforce like VsCode, Developer console and Workbench. Please check this post for PPT and further learning.
  1. Debugging Code in Salesforce
  2. Introduction to Integrated Developer Environment (IDE)
  3. Capabilities of various IDEs
  4. Demo
  5. Q & A

Day 4: Querying & Manipulating Salesforce Data


This session was a big session we divide the same into two part. Please check this post PPT and further learning link.

Basic of SOQL and SOSL in Salesforce | Query Plan 

All developer and admin should know the power of SOQL. In this session we covered the all below topic.
  1. What is SOQL
  2. Basics of SOQL
  3. Relationship Queries
  4. SOQL in Apex, Querying Records in Batches by Using SOQL for Loops
  5. Introduction to SOSL
  6. Using the right tool for the job SOQL Vs SOSL
  7. Writing optimal queries – Query Plan

Data Manipulation and Error Handling in Salesforce | DML

Here is agenda of session
  1. Data Manipulation 
  2. Insert Vs Database Insert 
  3. Error Handling in Salesforce

Day 5: Demystifying Apex Triggers


In this session we covered when should use the apex triggers. Please check this post to PPT and further learning link.
  1. Order of Execution
  2. When & How Triggers are invoked
  3. Trigger Events
  4. Avoiding Recursive Trigger Calls
  5. Q & A

 Salesforce trigger handler pattern 




If you want to learn more about Other Trigger framework and Order of execution then please check below recording.
  1. Order of Execution
  2. Apex trigger Framework.

 

Day 6: Unit Testing in Apex


This was one of the big session so we divided the session in two part. Please check this post for PPT and all further learning link

Writing Test Classes in Salesforce

 In this session we covered the basic of Test Classes.
  1. What are Unit tests
  2. Why Unit tests are needed
  3. What to Test
  4. TestSetup method in Test Class

Writing Effective Unit Test Classes.

Here is list for topic which covered in this recording.
  1. Write effective Unit tests
  2. Important thing to learn
  3. TestDataFactory class Unit tests


Day 7: Asynchronous Processing in Apex



Writing business logic in Apex that runs for a long duration is often a key ask by customers. In this episode we will learn about writing asynchronous process in Apex using Apex. Join us as you embark on this wonderful journey to become a champion Salesforce developer. Please check this post for PPT and further learning.

  1. Avenues of writing asynchronous processes
  2. Batch Processing
  3. Writing Scheduled Cron Jobs


  1. Future Methods
  2. Queueable
  3. Q & A


Please check this recording to learn more about Aysn Apex.

Day 8: Integrating with Salesforce


Salesforce is a service first platform and it is often required to integrate Salesforce with external applications and services. In this episode we will learn about performing callouts to external services using Apex. Join us as you embark on this wonderful journey to become a champion Salesforce developer. Check this post for PPT and recording.


  1. Web Communication Fundamentals
  2. Understanding REST Vs SOAP
  3. Message Exchange Formats – XML & JSON

  1. Basic of REST API
  2. HTTP Methods
  3. Performing Callouts to External Services


Day 9: Rest API vs Soap API and Enterprise & Partner


In this session we covered standard and custom REST API and how SOAP API worked. We also talk about Enterprise and partner WSDL. Please check this post for PPT and further learning.
  1. HTTP Request & Response
  2. Anatomy of a REST API CALL
  3. Standard REST API
  4. Custom REST API

  1. SOAP API
  2. SOAP Message
  3. Enterprise vs Partner WSDL
  4. SOAP vs REST
  5. Salesforce Limits
  6. Integration Patterns



Day 10: Introduction to Visualforce Page


In this session we talk about baisc of Visualforce page. Please check this post for PPT. Here is Agenda of session.
  • Introduction to Visualforce
  • MVP Pattern
  • Visualforce Constructs & Tags
  • Understanding Visualforce Controllers
    • Standard Controllers
    • Custom Controller
    • Exptensions
  • Capabilities & Considerations for Custom User Interfaces
  • Q & A

Day 11: Introduction to Lightning Components | AURA


In this session we talk about basic of lightning component and how to bulid custom UI with the help of AURA. Please check this post for PPT and further learning.
  1. Introduction to Lightning Aura Components
  2. Anatomy of a Lightning Aura Component
  3. Attributes and Expressions
  4. Communication with Salesforce
  5. Event in Aura
  6. Debugging Aura Components
  7. Styling UI with SLDS
  8. Q & A


Day 12: Introduction to Lightning web components


In this session we covered all below topic. Please check this post to PPT and further learning link.
  1. Introduction to Lightning Web Components
  2. Aura vs LWC
  3. Anatomy of a Lightning Web Component
  4. Lightning Playground
  5. Create your first Lightning web components
  6. Properties in LWC
  7. Invoking Apex from LwC
  8. Q & A


Day 13: Event Handling and Navigation IN LWC


In this session we covered all below topic. Please check this post for PPT.

  1.  Managing Navigation in LwC
  2. Working with Events
  3. Accessing Salesforce Data
  4. Using Custom Components in Lightning Experience
  5. Q & A





If this course helped then please like the recording and leave one comment for Speaker hard work.









Thanks
Amit Chaudhary

Sunday, 15 March 2020

World's First Salesforce Virtual Dreamin event

https://bit.ly/2TKFvcL

We are glad to announce the world first Salesforce Virtual Dreamin after a great success of Apex Hours, Automation Champion and Path To Code virtual program. All these events got fantastic response from various part of globe because it can be attended by anyone from anywhere.

There are so many fabulous Salesforce dreamins happening around the globe however there are some people who either are remote, not in a big city or just unable to travel. That’s why the above advantages of Virtual events are so important. There is an event named Virtual Dreamin and nows the time for you to join in. Don’t miss this one of a kind event.


"Geographical location is not an obstacle anymore. Join Dreamin from anywhere on planet"


5 Reasons Why Virtual Events Are The Future

  1. Join from Anywhere : A virtual event can overcome the limitations of brick and mortar events. You can connect with hundreds of people simultaneously. Whether you are at the grocery store or stuck in traffic. Just connect to the platform, settle in and meet people from all over the world.
  2. No Need to Travel & its Safe : No flight delays, security checks, or weather delays. If its virtual event, you can join from the comfort of your own home or office. All you need is an internet capable device with audio and visual capabilities and an internet connection. Check it out, Salesforce officially announced that its going virtual for Sydney world tour 2020 for safety concern.
  3. Easy on Budget : How about no more waiting on your VISA to go through, flight and hotel costs or waiting on your reimbursement check. Virtual events remove that barrier and allows you the freedom to explore without having to worry about the cost.
  4. More Outreach : No more geographical barriers. Since you can join these events from anywhere, means you are going to reach more participants and speakers. Use social media platforms to increase visibility to promote your event to reach more users with the added benefit of saving money.
  5. Quality Speakers : There are highly talented and inspiring speakers out there and getting those speakers can be impacted by the logistics of travel, budget, and time. If you are aspiring speaker, this is your chance to step up and show the world how capable you are.


3 Bonus reason to join Virtual Dreamin


  1. Its Free : Virtual Dreamin is free. Registration is now open for the World's First Salesforce Virtual Dreamin event on May 16-17, 2020. Register here https://bit.ly/2TKFvcL
  2. Its 24 Hours : This event is 24 hours event means you can join this from Timezone of your country.
  3. 3 Tracks running at same time : Three Track of sessions which include Admin, Devs & Architect. These tracks also includes Hands On Training (HoT), WIT and App Demo Jam. A Certified Technical Architect (CTA) is the pinnacle credential anyone can hold in Salesforce ecosystem. You can say, they are rare to find in Salesforce ecosystem like unicorns. However, we are grateful & honored as 5 CTA’s are taking time from their busy schedule and sharing knowledge with us.
https://bit.ly/2TKFvcL



Why To Sponsor us

Reach a Larger Global Audience
Extend your reach to audiences across the globe without the associated costs of airfare and travel. Host a wide range of online events from conferences to product launches with features like:
  • Registration capture
  • Support for international date/time formats

Mobile-Friendly Virtual Events
Whether your attendees are connecting with you on their phone or tablet, our platform supports all device and browser types.



Thank
Amit Chaudhary







Monday, 2 March 2020

Case Merge | Combine Duplicate Cases | Spring 20 Release Notes


Finally case merge feature is generally available from Sprint 20. Now agent can merge the duplicate cases like account and contacts. This functionality is not available in Classic. Even if you have added the button in standard button section, it will not appear on the page layout in Classic


To use the case merge you need to follow below step

1) Enable case merge

 First of all you need to enable the merge case feature from Feature Setting->Service-> Case Merge. Like below image.


 2) Add Button on case layout:


Once you have added case merge button on page Layout, You can check it by going to the record like below image


3) Select duplicate records

Once you click on the Merge Cases action It will open a popup to select the cases to be Merge.
You can select the 2 to 3 record at a time


4) Merge Record


Once you have selected it, Click on next. It will give you option to select your master case and master case field values



If your org chooses to delete merged cases, then only the master case is saved. The merged cases are soft deleted and are available in to the recycle bin for 15 days

Learn more

https://releasenotes.docs.salesforce.com/en-us/spring20/release-notes/rn_cases_case_merge_ga.htm

Friday, 24 January 2020

Enforce Security With the stripInaccessible Method


In this post we will talk about the new way to enforce the security in apex with stripInaccessible() method. From Winter 20, stripInaccessible() security feature for field-level data protection is available for beta in production. In winter 20 Salesforce extended the feature and added enum value UPSERTABLE to System.AccessType.

stripInaccessible() is useful to strip the field that current user don't have access from query and sub-query. We can use it to remove inaccessible field from sObjects before DML operation to avoid exceptions. This method also provides the option an option to enforce the Object level access check.

Syntax :- 


public static System.SObjectAccessDecision stripInaccessible(System.AccessType accessCheckType,
                                                             List<SObject> sourceRecords,
                                                             Boolean enforceRootObjectCRUD )
  • accessCheckType : This parameter determines the type of field-level access check to be performed
  • sourceRecords : A list of sObjects to be checked for fields that aren’t accessible in the context of the current user’s operation
  • enforceRootObjectCRUD : Indicates whether an object-level access check is performed
 

Use Case 1: No access on Field

In this example, User don't have access on field called accountNumber on Account Object.

        List<Account> accounts =[SELECT Id, Name,AccountNumber
                                 FROM Account limit 2];
         
        // Strip fields that are not readable
        SObjectAccessDecision decision = Security.stripInaccessible(
                                               AccessType.READABLE,
                                               accounts);
        
        // Print stripped records
        for (Integer i = 0; i < accounts.size(); i++) {
              System.debug('Insecure record access: '+accounts[i]);
              System.debug('Secure record access: '+decision.getRecords()[i]);
        }
        
        // Print modified indexes
        System.debug('Records modified by stripInaccessible: '+decision.getModifiedIndexes());
        
        // Print removed fields
        System.debug('Fields removed by stripInaccessible: '+decision.getRemovedFields());             
In above example user dont have access on accountNumber field. After using the Security.scripInaccessible method we can simply strip out the same field. Which all field are removed we can check with "getRemovedFields" method. Here is output of above code.



Use Case 2: No access on sObject

Let see another example when user don't have access on object itself. For demo I simply removed the access from Account object. Then we got the below exception.


 System.NoAccessException: No access to entity: Account
we have "enforceRootObjectCRUD" optional parameter which is true by default we can set that as false to get null value.


Use Case 3: SubQuery.

What about if you are try to access field from subquery ?

 List<Account> accountsWithContacts =
    [SELECT Name, AccountNumber,
        (SELECT LastName, Phone FROM Account.Contacts)
    FROM Account  limit 2];
 
// Strip fields that are not readable
   SObjectAccessDecision decision = Security.stripInaccessible(
                                       AccessType.READABLE,
                                       accountsWithContacts);

// Print stripped records
   for (Integer i = 0; i < accountsWithContacts.size(); i++)
  {
      System.debug('Insecure record access: '+accountsWithContacts[i]);
      System.debug('Secure record access: '+decision.getRecords()[i]);
   }

// Print modified indexes
   System.debug('Records modified by stripInaccessible: '+decision.getModifiedIndexes());

// Print removed fields
   System.debug('Fields removed by stripInaccessible: '+decision.getRemovedFields());
  This will remove the field on which user dont have access.



Use Case 4: DML.


What about if use dont have access on AccountNumber field and we will try to add value of AccountNumber by DML ?

public static void testDML(){
    Account acc = new Account(Name='Test', AccountNumber ='TestRating');
    insert acc ;
    System.debug('---->'+acc );
}   
This will insert the record with AccountNumber value in same user context even user dont have access on same field. How we can stop the same with Security.stripInaccessible. Let see

public static void testDML()
{  
    Account acc = new Account(Name='Test' ,AccountNumber ='Demo');
    System.debug('---->'+acc );
    List<Account> lstAcc = new List<Account>();
    lstAcc.add(acc);
   
    SObjectAccessDecision securityDecision = Security.stripInaccessible(
                                                AccessType.CREATABLE,
                                                lstAcc );
    System.debug('---ecurityDecision.getRecords()->'+securityDecision.getRecords());                                           
    insert securityDecision.getRecords();
    System.debug(securityDecision.getRemovedFields().get('Account'));
}      
here is output


You can also use the method to sanitize sObjects that have been deserialized from an untrusted source





Further Reading
  1. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_with_security_stripInaccessible.htm
  2. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Security.htm#topic-title


Sunday, 19 January 2020

Order of execution in Salesforce

We did one session in apex hours of How to become an Order of Execution Hero. In that session we did one deep drive in Salesforce Order of execution and covered some interview question on order of execution. Save Order of Execution is the most important factor in designing sustainable and scalable applications.
When you save a record with an insert, update, or upsert statement, Salesforce performs the following events in order. Here is cheat-sheet of Order of execution.

Order of Execution

  1. Loads Initial record.
  2. If the request came from a standard UI edit page, Salesforce runs system validation to check the record for page layout specific rules, field definition, Maximum field length.
  3. Executes  flows that make before-save update. (New Change in Winter 20)
  4. Executes all before triggers. 
  5. Runs most Custom validation.
  6. Executes duplicate rules. 
  7. Saves the record to the database, but doesn't commit yet. 
  8. Executes all after triggers. 
  9. Executes assignment rules. 
  10. Executes auto-response rules. 
  11. Executes workflow rules. 
  12. If there are workflow field updates, updates the record again.
  13. If workflow field updates introduced new duplicate field values, executes duplicate rules again. If the record was updated with workflow field updates, fires before update triggers and after update triggers one more time (and only one more time), in addition to standard validations. Custom validation rules are not run again.
  14. Executes processes and flow. 
  15. Executes escalation rules. 
  16. Executes entitlement rules. 
  17. If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through save procedure. 
  18. If the parent record is updated, and a grandparent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the grandparent record. Grandparent record goes through save procedure. 
  19. Executes Criteria Based Sharing evaluation. 
  20. Commits all DML operations to the database. 
  21. Executes all after-commit logic, such as sending email.

What is part of the after commit logic?

  1. All email sends
  2. Asynchronous Apex: @future methods
  3. Async Sharing Rule processing (for >25,000 records)
  4. Outbound Messages placed on queue
  5. Calculate Index, such as Search Index
  6. Render File Previews
  7. Publication of Platform Events (if configured)
NOTE: The order of execution isn’t guaranteed when having multiple triggers for the same object due to the same event. For example, if you have two before insert triggers for Case, and a new Case record is inserted that fires the two triggers, the order in which these triggers fire isn’t guaranteed.

Check this post for more detail Triggers and Order of Execution.

How deep is your knowledge of the Order of Execution ?

There's a lot more to know than triggers run before workflows. Dig deeper, understand the logic flow and your apps will scale better and run more reliably. Become an Order of Execution hero and walk away with an understanding of how to design robust apps optimized for scale and how to control recursion and remedy typical problems.  

What kind of issues did you experience ?

  • Unexpected Outcomes
    •  Missing or duplicate records
    •  Blank or “wrongly” populated fields
  • Hitting Limits
    •  CPU Time Limit Exception
    •  Too many SOQL Queries


Here is recording of our Apex Hour session.


 

Further Reading:

 https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm 


Thanks,
Amit Chaudhary

Friday, 3 January 2020

Lightning Datatable Sorting in Lightning Web Components

Lightning Datatable Sorting in Lightning Web Components
Last time we talk about Lightning Datatable in Lightning Web Components (LWC). In this post we will talk about lightning datatable example with sorting. We know lightning-datatable component displays in tabular data and each column can be displayed based on the data type. We can also achieve the column sorting with the help of onsort attribute in datatable. I hope VsCode is already setup on you machine and you know how to create Lightning Web Component. If not please check our Get started with Salesforce lightning web components post.

Lightning-datatable

Lightning datatable provides an onsort attribute which allow us to implement the sorting in lightning datatable. To enable the sorting on row you need to set sortable to true for the column and set sorted-By to match the fieldName attribute on the column. 

Use onsort event handler to update the table with the new column index and sort direction. The sort event returns the following parameter.
  1. fieldName    The fieldName that controls the sorting.
  2. sortDirection    The sorting direction. Valid options include 'asc' and 'desc'.
 You can implement sorting locally or via apex call.

Local Sorting:

We mostly implement this type of sorting when we know data elements in lightning datatable is small and limited.

1) Create Apex Class. 

To select certain contacts using SOQL, use an Apex method. Check this post to learn about how to Call Apex Methods in LWC.

LWCDataTableSortingExample:
public with sharing class LWCDataTableSortingExample {
    @AuraEnabled(Cacheable=true)
    public static List <Contact> getContacts() {
        List<Contact> contList = [ SELECT Id, FirstName, LastName, Phone, Email
                                   FROM Contact
                                   LIMIT 10 ];
        return contList;
    }     
}

2) Create Lightning web component.

dataTableSortingLWC.html
<template>
    <lightning-card title="Data Sorting in Lightning Datatable in LWC" icon-name="standard:contact" >
        <br/>
        <div style="width: auto;">
            <template if:true={data}>
                <lightning-datatable data={data}
                                     columns={columns}
                                     key-field="id"
                                     sorted-by={sortBy}
                                     sorted-direction={sortDirection}
                                     onsort={doSorting}
                                     hide-checkbox-column="true"></lightning-datatable>
            </template>
        </div>
    </lightning-card>
</template>


dataTableSortingLWC.js
import {LightningElement, wire, track} from 'lwc';
import getContacts from '@salesforce/apex/LWCDataTableSortingExample.getContacts';

// datatable columns with row actions. Set sortable = true
const columns = [ { label: 'FirstName', fieldName: 'FirstName', sortable: "true"},
                  { label: 'LastName', fieldName: 'LastName', sortable: "true"},
                  { label: 'Phone', fieldName: 'Phone', type: 'phone', sortable: "true"},
                  { label: 'Email', fieldName: 'Email', type: 'email', sortable: "true" },];

export default class DataTableSortingLWC extends LightningElement {
    @track data;
    @track columns = columns;
    @track sortBy;
    @track sortDirection;
  
    @wire(getContacts)
    contacts(result) {
        if (result.data) {
            this.data = result.data;
            this.error = undefined;
        } else if (result.error) {
            this.error = result.error;
            this.data = undefined;
        }
    }

    doSorting(event) {
        this.sortBy = event.detail.fieldName;
        this.sortDirection = event.detail.sortDirection;
        this.sortData(this.sortBy, this.sortDirection);
    }

    sortData(fieldname, direction) {
        let parseData = JSON.parse(JSON.stringify(this.data));
        // Return the value stored in the field
        let keyValue = (a) => {
            return a[fieldname];
        };
        // cheking reverse direction
        let isReverse = direction === 'asc' ? 1: -1;
        // sorting data
        parseData.sort((x, y) => {
            x = keyValue(x) ? keyValue(x) : ''; // handling null values
            y = keyValue(y) ? keyValue(y) : '';
            // sorting values based on direction
            return isReverse * ((x > y) - (y > x));
        });
        this.data = parseData;
    }      
}

dataTableSortingLWC.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="dataTableSortingLWC">
    <apiVersion>46.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

 

Sorting by Apex Call:

We also have another way of data sorting with Apex class.

1) Create Apex Class. 

LWCDataTableSortingExample:
public with sharing class LWCDataTableSortingExample {
    @AuraEnabled(Cacheable=true)
    public static List <Contact> getContacts(String field, String sortOrder) {
        String query;
        query  = 'SELECT Id, FirstName, LastName, Phone, Email FROM Contact';
        if(field != null && sortOrder !=null){
            query += ' ORDER BY '+field+' '+sortOrder;
        }
        return Database.query(query);
    }
}

2) Create Lightning web component.

dataTableSortingLWC.html
<template>
    <lightning-card title="Data Sorting by Apex" icon-name="standard:contact" >
        <br/>
        <div style="width: auto;">
            <template if:true={data}>
                <lightning-datatable data={data}
                                     columns={columns}
                                     key-field="id"
                                     sorted-by={sortBy}
                                     sorted-direction={sortDirection}
                                     onsort={doSorting}
                                     hide-checkbox-column="true"></lightning-datatable>
            </template>
        </div>
    </lightning-card>
</template>

dataTableSortingLWC.js
import {LightningElement, wire, track} from 'lwc';
import getContacts from '@salesforce/apex/LWCDataTableSortingExample.getContacts';

// datatable columns with row actions
const columns = [ { label: 'FirstName', fieldName: 'FirstName', sortable: "true"},
                  { label: 'LastName', fieldName: 'LastName', sortable: "true"},
                  { label: 'Phone', fieldName: 'Phone', type: 'phone', sortable: "true"},
                  { label: 'Email', fieldName: 'Email', type: 'email', sortable: "true" },];

export default class DataTableSortingLWC extends LightningElement {
    // reactive variable
    @track data;
    @track columns = columns;
    @track sortBy='FirstName'
    @track sortDirection='asc';
  
    // retrieving the data using wire service
    @wire(getContacts,{field : '$sortBy',sortOrder : '$sortDirection'})
    contacts(result) {
        if (result.data) {
            this.data = result.data;
            this.error = undefined;
        } else if (result.error) {
            this.error = result.error;
            this.data = undefined;
        }
    }

    doSorting(event) {
        // calling sortdata function to sort the data based on direction and selected field
        this.sortBy = event.detail.fieldName;
        this.sortDirection = event.detail.sortDirection;
        this.sortData(this.sortBy, this.sortDirection);
    }

    sortData(fieldname, direction) {
        let parseData = JSON.parse(JSON.stringify(this.data));
        // Return the value stored in the field
        let keyValue = (a) => {
            return a[fieldname];
        };
        // cheking reverse direction
        let isReverse = direction === 'asc' ? 1: -1;
        // sorting data
        parseData.sort((x, y) => {
            x = keyValue(x) ? keyValue(x) : ''; // handling null values
            y = keyValue(y) ? keyValue(y) : '';
            // sorting values based on direction
            return isReverse * ((x > y) - (y > x));
        });

        // set the sorted data to data table data
        this.data = parseData;
    }      
}


Please check below post on Lightning Web Components:-

Thanks
Amit Chaudhary