Play Games

Search This Blog

Thursday, January 6, 2022

How to get the character or characters of a string that repeated most number of times - Salesforce Globe For You

How to get the character or characters of a string that repeated most number of times - Salesforce Globe For You

Solution

Step 1: convert the given string in to an array which holds a single character in each index

Step 2:Prepare a map with Key as character and value as number of times it repeated by iterating the above array based on its size.

Step 3: Get the maximum count any character repeated most by iterating the above map.

Step 4:Iterate the map and get the characters by comparing each value of map with the maximum count calculated in the above step.

Sample Code:

Run the following sample code in developer console

String name ='Salesforce Globe For You';

Map<String,Integer> mapCharacter = new Map<String,Integer>();

//convert name to Character array

List<String> lstCharacter = name.split('');

for(integer i=0; i<lstCharacter.size(); i++) {

    if(string.isNOTBLANK(lstCharacter[i])) {

        String lowerCaseCharacter = lstCharacter[i].toLOWERCASE();

        if(mapCharacter.containsKey(lowerCaseCharacter)) {

            Integer count = mapCharacter.get(lowerCaseCharacter)+1;

            mapCharacter.put(lowerCaseCharacter,count);

        } else {

            mapCharacter.put(lowerCaseCharacter,1);

        }

    }

}

//To get the maximum number of times a character repeated

String mostRepeatedCharacter ='';

integer charMaxRepeatedcount = 0;

for(String s:mapCharacter.keyset()) {

    If(mapCharacter.get(s) > charMaxRepeatedcount) {

        charMaxRepeatedcount = mapCharacter.get(s);

    }

}

// To get Character that repeated most

for(String s:mapCharacter.keyset()) {

    If(mapCharacter.get(s) == charMaxRepeatedcount) {

        if(mostRepeatedCharacter =='') {

            mostRepeatedCharacter = s;

        } else {

            mostRepeatedCharacter = mostRepeatedCharacter+', '+s;

        }

        

    }

}

system.debug('Most Repeating Character or Characters in given String : '+mostRepeatedCharacter);

Output:



How do update a salesforce record from Lightning Web Component without using Apex - Salesforce Globe For You

How do update a salesforce record from Lightning Web Component without using Apex - Salesforce Globe For You

Solution: Import the updateRecord from 'lightning/uiRecordApi' to update a record.

Example: Assume lead id is '00Q0o00001ngIlLEAU' and lead last name is 'UpdateRecord'

updateRecordWithoutApexInLWC.html

<template>

    <lightning-card title="Update Lead Record Without Apex" icon-name="standard:record">

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

                Lead Id:{leadId} 

        </div>

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

                Initial Lead Last Name:{name} 

        </div>

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

        <lightning-button label="Update Lead" variant="brand" onclick={updateLead}></lightning-button>

    </div>

    </lightning-card>

</template>

updateRecordWithoutApexInLWC.js

import { updateRecord } from 'lightning/uiRecordApi';

import { LightningElement, api } from 'lwc';

import LASTNAME_FIELD from '@salesforce/schema/Lead.LastName';

import ID_FIELD from '@salesforce/schema/Lead.Id';

import { ShowToastEvent } from 'lightning/platformShowToastEvent';

import { NavigationMixin } from 'lightning/navigation';

export default class UpdateRecordWithoutApexInLWC extends NavigationMixin(LightningElement) {

    @api leadId ='00Q0o00001ngIlLEAU';

    @api name ='UpdateRecord';

    updateLead() {

        const fields = {};

        fields[ID_FIELD.fieldApiName] = this.leadId;

        fields[LASTNAME_FIELD.fieldApiName] = 'Name Updated';

        const recordInput = { fields };

        updateRecord(recordInput)

            .then(() => {

                this.dispatchEvent(

                    new ShowToastEvent({

                        title: 'Success',

                        message: 'Lead Last Name Updated Successfully',

                        variant: 'success'

                    })

                );

                this[NavigationMixin.Navigate]({

                    type: 'standard__recordPage',

                    attributes: {

                        recordId: this.leadId,

                        objectApiName: 'Lead',

                        actionName: 'view'

                    }

                });

            })

            .catch(error => {

                console.log('Error',error);

                this.dispatchEvent(

                    new ShowToastEvent({

                        title: 'Error Updating record',

                        message: error.body.message,

                        variant: 'error'

                    })

                );

            });

    }

}

updateRecordWithoutApexInLWC.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:



Wednesday, January 5, 2022

How to retrieve all the global picklist value sets in vs code using package.xml - Salesforce Globe For You

How to retrieve all the global picklist value sets in vs code using package.xml - Salesforce Globe For You

Solution: Use the below code in package.xml to retrieve global value sets in vs code

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

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

    <types>

        <members>*</members>

        <name>GlobalValueSet</name>

    </types>

    <version>52.0</version>

</Package>

Assume 'Alphabet' Global Value set is created as shown in above image.

Output:



Tuesday, January 4, 2022

How to show dropdown or select list with salesforce records as options in Lightning Web Component(LWC) - Salesforce Globe For You

How to show dropdown or select list with salesforce records as options in Lightning Web Component(LWC) - Salesforce Globe For You

Solution: Use <lightning-combobox> to display select list in LWC

Example:

Apex Class: LeadDataController

public class LeadDataController {

    @AuraEnabled

    public static List<Lead> fetchLeads() {

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

        lstLead = [Select id,name from Lead limit 5];

        return lstLead;

    }

}

displayDropDownInLWC.html

<template>

    <lightning-card>

    <lightning-combobox

            name="progress"

            label="Display Top 5 Lead Records in Drop Down"

            value={selectedLead}

            placeholder="Select Lead Record"

            options={lstLeadRecordOptions}

            onchange={handleChange} ></lightning-combobox>


    <p>Selected value is: {selectedLead}</p>

    </lightning-card>

</template>

displayDropDownInLWC.js:

import { track,LightningElement } from 'lwc';

import fetchLeads from '@salesforce/apex/LeadDataController.fetchLeads';

export default class DisplayDropDownInLWC extends LightningElement {

    @track lstLeadRecordOptions=[];

    @track selectedLead;

    connectedCallback() {

        fetchLeads()

        .then(result => {

            if(result){

                const temp = { label: 'None', value: 'None' };

                this.lstLeadRecordOptions = [ ...this.lstLeadRecordOptions, temp ];

                for(var item of result) {

                    var lead = { label: item.Name, value: item.Id };

                    this.lstLeadRecordOptions = [ ...this.lstLeadRecordOptions, lead ];

                }

            }

        })

        .catch(error => {

            this.error = error;

        });

    }

    handleChange(event) {

        this.selectedLead = event.detail.value;

    }

}

displayDropDownInLWC.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__AppPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>

Output: