Play Games

Search This Blog

Friday, August 19, 2022

formula to calculate days between two dates in salesforce - Salesforce lobe For You

Solution: If both dates are of date datatype,then directly subtract start date from end date field to get the number of days between 2 dates.

Example: Start Date and End Date are date fields on Lead Object.



The formula field (No.of days) will be like this 

Output:


If both are of datetime datatype fields,then also directly subtract start date time from end date time field to get the number of days between 2 dates.

Example: Start Date and time and End Date and Time are date time fields on Lead Object.



The formula field (No.of days) will be like this 

Output:



query to filter records based on multiselect picklist values in apex salesforce - Salesforce Globe For You

Solution: Use includes in query to filter based on multiselect picklist field with values separated by comma(,) or semicolon (;)

Use semicolon(;) to specify AND operation. Example ('A;B') means record where A and B values are selected.

Use comma (,) to specify OR operation.Example ('A','B') means record where A or B are selected.

Assume Alphabet is a multi select picklist field created on Lead object with values from A to Z 

SOQL query to fetch lead records where A and B multiselect picklist values are selected 

Select id,name from Lead where Alphabet__c includes ('A;B')

Output:

SOQL query to fetch lead records where A or B or both multiselect picklist values are selected 

Select id,name,Alphabet__c from Lead where Alphabet__c includes ('A','B')

Output:



Thursday, August 18, 2022

Trigger.operationType in salesforce - Salesforce Globe For You

OperationType is a Trigger class context variable which returns an enum of type System.TriggerOperation corresponding to the current operation.

Possible values of the System.TriggerOperation enum are: 

BEFORE_INSERT

BEFORE_UPDATE

BEFORE_DELETE

AFTER_INSERT

AFTER_UPDATE

AFTER_DELETE and 

AFTER_UNDELETE.

Trigger.operationType is used in trigger to determine the type of operation whether it is before insert or before update etc.

Example:

trigger LeadTrigger on Lead ( before Insert ,Before Update , After Insert, After Update) {

    system.debug('Trigger.OperationType :' +Trigger.OperationType);

}

Now when I update any one lead record ,the output is as shown below


It means when the lead record is updated, the lead trigger runs and the Trigger.OperationType tells that the trigger is running for the before update and after update context.

Monday, August 8, 2022

Rename salesforce trailhead palyground org - SalesforceGlobe4U

Solution: Login to Salesforce trailhead account and navigate to bottom of the screen where the launch section is visible as shown below

click on 3 dots and click on manage Orgs link


It will navigate to the connected orgs screen as shown below


click rename to rename specific org and save.

Display prime numbers with in the range

Solution: A prime number is a number that is only divisible by 1 and itself.

Example: 2, 3, 5, 7, 11 etc are prime numbers

Example:

Apex class:

public class PrimeNumberController {

    public static void displayPrimeNumbers(Integer startingNumber,Integer endNumber) {

        List<Integer> lstPrimeNumber = new List<Integer>();

        for(integer i= startingNumber ; i<=endNumber ; i++) {

            if(checkPrimeNumber(i) == true) {

                lstPrimeNumber.add(i);

            }

        }

        System.debug('The Prime Numbers are: '+lstPrimeNumber);

    }

    public static Boolean checkPrimeNumber(integer num) {

        Boolean isPrimeNumber = true;

        if(num <2) {

            isPrimeNumber = false;

        } else {

            for(integer i = 2; i <num; i++) {

                integer m = math.mod(num,i);

                if(math.mod(num,i) == 0) {

                    isPrimeNumber = false;

                }

            }

        }

        return isPrimeNumber;

    }

}

Run the following from developer console

PrimeNumberController.displayPrimeNumbers(40,50);

Output:



Wednesday, August 3, 2022

Pass current page record id to wire method in Lightning Web Component - Salesforce Globe For You

Solution: @api recordId property will fetch the record id from the detail record page where this component is placed.

Use '$recordId' to pass current page record id to wire method inside the js file.

Example:

passRecordIdToWireInLWC.html:

<template>

    <lightning-card title="Get Related List Contact Records">

        <template if:true={records}>

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

                <template for:each={records} for:item="rec">

                    <p key={rec.fields.Id.value}>

                        {rec.fields.Name.value}

                    </p>

                </template>

            </div>

        </template>

    </lightning-card>

</template>

passRecordIdToWireInLWC.js:

import { LightningElement, wire ,api } from 'lwc';

import { getRelatedListRecords } from 'lightning/uiRelatedListApi';

export default class PassRecordIdToWireInLWC extends LightningElement {

    @api recordId;

    error;

    records;

    @wire(getRelatedListRecords, {

        parentRecordId: '$recordId',

        relatedListId: 'Contacts',

        fields: ['Contact.Id','Contact.Name']

    })listInfo({ error, data }) {

        if (data) {

            this.records = data.records;

            this.error = undefined;

        } else if (error) {

            this.error = error;

            this.records = undefined;

        }

    }

}

passRecordIdToWireInLWC.js-meta.xml:

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

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

    <apiVersion>55.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__RecordPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>

Output: