Monday, October 21, 2019

How to get Current Logged in User information in Lightning Web Component(lwc)

This post explains how to get logged in user information in lightning web components(lwc)



check below post how to import current user id in lightning web components
https://www.salesforcecodecrack.com/2019/01/how-to-import-current-user-id.html

To get the record details to use the getRecord wire adapter from 'lightning/uiRecordApi' scope module.
Syntax
import { getRecord } from 'lightning/uiRecordApi';

HTML code
<template>
    <lightning-card title="Current Logged User Information">
        <template if:true={objUser}>
            <dl class="slds-list_horizontal slds-wrap" style="margin-left: 3%;">
                <dt class="slds-item_label slds-truncate" title="First Name">First Name:</dt>
                <dd class="slds-item_detail slds-truncate"><b>{objUser.FirstName}</b></dd>
                <dt class="slds-item_label slds-truncate" title="Last Name">Last Name:</dt>
                <dd class="slds-item_detail slds-truncate"><b>{objUser.LastName}</b></dd>
                <dt class="slds-item_label slds-truncate" title="Full Name">Name:</dt>
                <dd class="slds-item_detail slds-truncate"><b>{objUser.Name}</b></dd>
                <dt class="slds-item_label slds-truncate" title="Alias">Alias:</dt>
                <dd class="slds-item_detail slds-truncate"><b>{objUser.Alias}</b></dd>
                <dt class="slds-item_label slds-truncate" title="Active">Is Active:</dt>
                <dd class="slds-item_detail slds-truncate"><b>{objUser.IsActive}</b></dd>
            </dl>
        </template>
    </lightning-card>
</template>

Javascript Controller 
import { LightningElement, track, wire} from 'lwc';

// importing to get the record details based on record id
import { getRecord } from 'lightning/uiRecordApi';

// impoting USER id
import USER_ID from '@salesforce/user/Id'; 

export default class CurrentUserDetailsInLWC extends LightningElement {

    @track objUser = {};
    
    // using wire service getting current user data
    @wire(getRecord, { recordId: USER_ID, fields: ['User.FirstName', 'User.LastName', 'User.Name', 'User.Alias', 'User.IsActive'] })
    userData({error, data}) {
        if(data) {
            window.console.log('data ====> '+JSON.stringify(data));

            let objCurrentData = data.fields;

            this.objUser = {
                FirstName : objCurrentData.FirstName.value,
                LastName : objCurrentData.LastName.value,
                Name : objCurrentData.Name.value,
                Alias : objCurrentData.Alias.value,
                IsActive : objCurrentData.IsActive.value,
            }
        } 
        else if(error) {
            window.console.log('error ====> '+JSON.stringify(error))
        } 
    }

}

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

Result

Saturday, October 19, 2019

How to test salesforce performance and speed in Lightning Experience

If your Users are experiencing slow page-loading times when using Lightning Experience, it may be related to one or more of the following issue types.
  • Geographical
  • Device
  • Browser
  • Salesforce organization configuration issues

It is a simple online tool to measure your browser Javascript speed and your internet connection speed to Salesforce’s servers. 

How to access the tool?

To access this tool simply log-on to your Salesforce org and remove everything after “[your domain name].lightning.force.com/” in the browser URL and add the speedtest.jsp 
Ex:
https://<your domain>.lightning.force.com/speedtest.jsp
OR
https://[instance].salesforce.com/speedtest.jsp


Click the "Test Speed" button, tool tests all the performances, Once the test is finished, results will be displayed.

Reference
https://help.salesforce.com/articleView?id=000316034&language=en_US&type=1&mode=1

Thursday, October 17, 2019

Custom Data table with multiple row selection in Lightning Web Components(lwc)

This post explains how to implement custom data table with multiple row selection in Lightning Web Component(lwc)



customDatatableInLWC.html
<template>
    <lightning-card title="Custom Data table in Lightning Web Components">
        <div class="slds-grid slds-gutters">
            <div class="slds-col">
                <span></span>
            </div>
            <div class="slds-col">
                <span>
                    <lightning-button label="Show Selected Contacts" onclick={showContacts} style="margin-left: 40%" variant="brand"></lightning-button>
                </span>
            </div>
        </div><br/>
        <table class="slds-table slds-table_cell-buffer slds-table_bordered">
            <thead>
                <tr class="slds-line-height_reset">
                    <th class="" scope="col">
                        <div class="slds-truncate">
                            <lightning-input onchange={allSelected} type="checkbox"></lightning-input>
                        </div>
                    </th>
                    <th class="" scope="col">
                        <div class="slds-truncate" title="Name">Name</div>
                    </th>
                    <th class="" scope="col">
                        <div class="slds-truncate" title="First Name">First Name</div>
                    </th>
                    <th class="" scope="col">
                        <div class="slds-truncate" title="Last Name">Last Name</div>
                    </th>
                    <th class="" scope="col">
                        <div class="slds-truncate" title="Phone">Phone</div>
                    </th>
                    <th class="" scope="col">
                        <div class="slds-truncate" title="Email">Email</div>
                    </th>
                </tr>
            </thead>
            <tbody>
                <template for:each={data} for:item="con">
                    <tr key={con.Id}>
                        <th scope="col">
                            <div>
                                <lightning-input type="checkbox" value={con.Name} data-id={con.Id}></lightning-input>
                            </div>
                        </th>
                        <th scope="col">
                            <div>{con.Name}</div>
                        </th>
                        <th scope="col">
                            <div>{con.FirstName}</div>
                        </th>
                        <th scope="col">
                            <div>{con.LastName}</div>
                        </th>
                        <th scope="col">
                            <div>
                                <lightning-formatted-phone value={con.Phone}></lightning-formatted-phone>
                            </div>
                        </th>
                        <th scope="col">
                            <div>
                                <lightning-formatted-email value={con.Email}></lightning-formatted-email>
                            </div>
                        </th>
                    </tr>
                </template>
            </tbody>
        </table>

        <!--
            This modal box used to show the selected contacts
        -->

        <div if:true={selectedCons}>
            <template if:true={bShowModal}>
                <section aria-describedby="modal-content-id-1" aria-labelledby="modal-heading-01" aria-modal="true" class="slds-modal slds-fade-in-open" role="dialog" tabindex="-1">
                    <div
                        class="slds-modal__container">
                        <!-- modal header start -->
                        <header class="slds-modal__header">
                            <button class="slds-button slds-button_icon slds-modal__close slds-button_icon-inverse" onclick={closeModal} title="Close">
                                <lightning-icon alternative-text="close" icon-name="utility:close" size="small" variant="inverse"></lightning-icon>
                                <span class="slds-assistive-text">Close</span>
                            </button>
                            <h2 class="slds-text-heading_medium slds-hyphenate" id="modal-heading-01">Selected Contacts Records</h2>
                        </header>
                        <!-- modal body start -->
                        <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
                            <template for:each={selectedCons} for:item="con">
                                <dl class="slds-list_horizontal slds-wrap" if:true={con.Id} key={con.Id}>
                                    <dt class="slds-item_label slds-truncate" title="First Name">Contact Name:</dt>
                                    <dd class="slds-item_detail slds-truncate">{con.Name}</dd>
                                    <dt class="slds-item_label slds-truncate" title="LastName">Contact Id:</dt>
                                    <dd class="slds-item_detail slds-truncate">{con.Id}</dd>
                                    <hr></dl>
                                </template>

                            </div>
                            <!-- modal footer start-->
                            <footer class="slds-modal__footer">
                                <button class="slds-button slds-button_neutral" onclick={closeModal}>Cancel</button>
                            </footer>
                        </div>
                    </section>
                    <div class="slds-backdrop slds-backdrop_open"></div>
                </template>
            </div>
        </lightning-card>
    </template>

customDatatableInLWC.js
import { LightningElement, track, wire } from 'lwc';

// importing Apex Class
import retriveCons from '@salesforce/apex/LWCExampleController.getContacts';

export default class CustomHTMLDatatable extends LightningElement {
    // reactive variables
    @track data = [];
    @track error;
    @track bShowModal = false;
    @track selectedCons;

    // opening the modal
    openModal() { this.bShowModal = true; }
    // closeing the modal
    closeModal() { this.bShowModal = false;}

    // Getting Contacts using Wire Service
    @wire(retriveCons)
    contacts(result) {
        if (result.data) {
            this.data = result.data;
            this.error = undefined;

        } else if (result.error) {
            this.error = result.error;
            this.data = undefined;
        }
    }

    // Select the all rows
    allSelected(event) {
        let selectedRows = this.template.querySelectorAll('lightning-input');
        
        for(let i = 0; i < selectedRows.length; i++) {
            if(selectedRows[i].type === 'checkbox') {
                selectedRows[i].checked = event.target.checked;
            }
        }
    }

    showContacts() {
        this.bShowModal = true;

        this.selectedCons = [];

        let selectedRows = this.template.querySelectorAll('lightning-input');

        // based on selected row getting values of the contact
        for(let i = 0; i < selectedRows.length; i++) {
            if(selectedRows[i].checked && selectedRows[i].type === 'checkbox') {
                this.selectedCons.push({
                    Name: selectedRows[i].value,
                    Id: selectedRows[i].dataset.id
                })
            }
        }
    }

}

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


Apex Class
public inherited sharing class LWCExampleController {    
 @AuraEnabled(Cacheable = true)
    public static List<Contact> getContacts(){
        return [SELECT Id, Name, FirstName,LastName, Phone, Email FROM Contact limit 10];
    }
}

Result