Play Games

Search This Blog

Saturday, November 20, 2021

How to parse the data stored as map(key,value) in custom Label in Apex - Salesforce Globe For You

How to parse the data stored as map(key,value) in custom Label in Apex - Salesforce Globe For You

Problem: Assume that the data in the custom label stored as shown below

key1:value1;key2:value2;key3:value3

Now get the value from custom label and prepare map.

Solution: Get the value from Label and then split using ';' and store it in string array.Now iterate that array and do split using ':' and finally prepare map.

Example: The MapValues label is created as shown below


Sample Code:

List<string> lstValues = system.label.MapValues.split(';');

Map<String,string> mapValues = new Map<String,string>();

for(String val : lstValues){

    list<String> lstVal = val.split(':');

    mapValues.put(lstVal[0],lstVal[1]);

}

system.debug('mapValues:'+mapValues);

system.debug('keyset:'+mapValues.keyset());

system.debug('values:'+mapValues.values());

Output:



How to hide/remove label(active/inactive) from toggle button in Lightning web component - Salesforce Globe For You

How to hide/remove label(active/inactive) from toggle button in Lightning web component - Salesforce Globe For You

Solution: Set the attributes message-toggle-active and message-toggle-inactive to empty to remove the labels from Toggle button as shown below

<lightning-input message-toggle-active ="" message-toggle-inactive="" type="toggle" label="Toggle Checkbox" name="input"></lightning-input>

Example:

hideToggleButtonLabelLWC.html

<template>

    <lightning-card title="Toggle Button by default in LWC Component">

        <lightning-input type="toggle" label="Toggle Button" name="input"></lightning-input>

    </lightning-card><br/>

    <lightning-card title="Toggle Button after making both attributes to empty in LWC Component">

        <lightning-input type="toggle" label="Toggle Button" name="input" message-toggle-active ="" message-toggle-inactive=""></lightning-input>

    </lightning-card>

</template>

hideToggleButtonLabelLWC.js

import { LightningElement } from 'lwc';

export default class ToggleButtonLWC extends LightningElement {}

hideToggleButtonLabelLWC.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>48.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__RecordPage</target>

        <target>lightning__AppPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>

Output:



Thursday, November 11, 2021

How to hide and show specific record details when multiple records are displayed based on click in Lightning Web component - Salesforce Globe for You

How to hide and show specific record details when multiple records are displayed based on click in Lightning Web component  -  Salesforce Globe for You

Problem: Assume a list of Lead records are displayed using LWC Component.Initially  on load only 1 field of each record will be visible.If your want to see more fields,he can click 'Show More' to view more data and then he can click 'View Less' to hide the details.

Solution: In the list of records itself,introduce one boolean field to handle the hide and show details.When user clicks on specific record,change that record related boolean flag to toggle as done in the example below

Example:

Apex class:

public class LeadController {

    @AuraEnabled

    public static List<LeadData> getLeads() {

        List<LeadData> lstLead = new List<LeadData>();

        for(Lead objLead : [Select id,name,leadSource from Lead Limit 3]) {

            LeadData objLeadData = new LeadData();

            objLeadData.ShowDetails = false;

            objLeadData.lead = objLead;

            lstLead.add(objLeadData);

        }

        return lstLead;

    }

    public Class LeadData {

        @AuraEnabled public Boolean ShowDetails {get;set;}

        @AuraEnabled public Lead lead {get;set;}

    }

}

showHideLWC.html:

<template>

    <template for:each={lstLeadData} for:item="objLeadData">

        <lightning-card key={objLeadData.lead.Id}>

            <template if:false={objLeadData.ShowDetails} key={objLeadData.lead.Id}>

                    Lead: {objLeadData.lead.Name} <span data-id={objLeadData.lead.Id}  onclick={handleToggle}><a>Show More</a></span>

            </template>

            <template if:true={objLeadData.ShowDetails}>

                    Lead: {objLeadData.lead.Name} <span data-id={objLeadData.lead.Id}  onclick={handleToggle}><a>View Less</a></span>

                <div>

                    <div>Name :{objLeadData.lead.Name}</div>

                    <div>Lead Id :{objLeadData.lead.Id}</div>

                </div>

            </template>

        </lightning-card>

    </template>

</template>

showHideLWC.js:

import { LightningElement,track } from 'lwc';

import getLeads from '@salesforce/apex/LeadController.getLeads';

export default class ShowHideLWC extends LightningElement {

    @track lstLeadData;

    connectedCallback() {

        getLeads()

            .then(result => {

                this.lstLeadData = result;

                console.dir(result);

                console.table(result);

                console.log(result);

            })

            .catch(error => {

                this.error = error;

            });

    }

    handleToggle(event) {

        var leadId = event.currentTarget.dataset.id;

            this.lstLeadData.forEach(function(objLeadData){

                if(objLeadData.lead.Id == leadId) {

                    objLeadData.ShowDetails = !objLeadData.ShowDetails;

                }

               

            });

    }

}

showHideLWC.js-meta.xml:

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>52.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__RecordPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>

Output:



Thursday, November 4, 2021

How to display toggle button in Lightning web component - Salesforce Globe For You

Solution: Use lightning-input with type= "toggle" to display toggle button as shown below

<lightning-input type="toggle" label="Toggle Checkbox" name="input"></lightning-input>

Example:

toggleButtonLWC.html

<template>

    <lightning-card title="Toggle Button in LWC Component">

        <lightning-input type="toggle" label="Toggle Button" name="input"></lightning-input>

    </lightning-card>

</template>

toggleButtonLWC.js

import { LightningElement } from 'lwc';

export default class ToggleButtonLWC extends LightningElement {}

toggleButtonLWC.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>48.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__RecordPage</target>

        <target>lightning__AppPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>

Output:



Monday, November 1, 2021

If condition in lightning web component - Salesforce Globe For You

If condition in lwc component - Salesforce Globe For You

Solution: <template if:true={condition}> </template> 

Example:

ifConditionLWC.html

<template>

    <lightning-card title="If condition LWC Component">

        <div class="slds-m-around_medium">

            <template if:true={bTrue}>

                <div class="slds-m-vertical_medium">

                    This Will display only when boolean variable bTrue is true.

                </div>

            </template>

        </div>

    </lightning-card>

</template>

ifConditionLWC.js

import { LightningElement,track } from 'lwc';

export default class IfConditionLWC extends LightningElement {

    @track bTrue = true;

}

ifConditionLWC.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>48.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__RecordPage</target>

        <target>lightning__AppPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>

Output: