Sunday, September 3, 2023

SOSL In Salesforce

 

Introduction to SOSL 

Salesforce Object Search Language (SOSL) is used to write queries to search text against the search index.

Always create filters that are selective, when building efficient SOSL queries. By default, SOSL queries scan all entities. The search engine can return maximum of 2,000 records after matching the searched term or text.

SOSL statements are basically used to find or search the text format data, email, and phone fields for multiple objects, including custom objects, that we have access to in a single query.


SOSL Syntax: 

FIND {Search Text} [IN Name/Email/Phone/All] RETURNING sObject(Fields to return);
Example: SOSL to search 'Pepsi' text in all fields and return Account Object’s name, industry as result.

FIND {Pepsi} IN ALL FIELDS RETURNING Account (Name, Industry);

Note: We can use multiple objects as returning data in SOSL.

Example: SOSL to search ‘Pepsi’ text in all fields and return Account Object’s name, industry and Contact Object’s name, email as result.

FIND {Pepsi} IN ALL FIELDS RETURNING Account (Name, Industry), Contact (Name, Email);

Saturday, September 2, 2023

GIT Commands To Commit Salesforce Changes Using Command Prompt

 


1.  Download the git. To download click on this link:

https://git-scm.com/downloads


2.  After the installation, open Command Prompt and check whether git is installed properly or not. Run this command:

git --version 


3.  Now Go to a directory where you want to clone the project from Git. Run Command:

cd foldername


4.  Next step is to initialize the Git in that folder. Run Command:

git init


5.  After the initialization is completed, then we need to clone the project from Git into our local desktop. Run Command:

git clone https://gitlab.eng.abc.com/SFstar/it-sfdc.git

(You can get this URL from your git repository)


6.  Go inside the last directory which is it-sfdc in my case. Run Command:

cd it-sfdc


7.  Next, checkout our feature branch where we need to commit our changes for the upcoming release. Run Command:

git checkout FeatureBranch_Release_12Dec2023


8.  Once the feature branch is checked out it is always recommended to pull the changes. Or lets say someone has checked out this feature branch yesterday and today he is adding his components to that branch. In that case he needs to pull the changes if some other developer has added his own components. Run Command:

git pull


9.  Now start adding your changes to the local files or replace your files with the modified code.


10. Check the status of modified files. Run Command:

Git status

At this time, we will see all the modified files names in 

red color.


11. If you have completed the modification, its time to add them. Run Command:

git add force-app/main/default/objects/ABC__c/

Or

git add .


Note: first command will add only the particular component 

i.e. ABC object but the second command will add all the 

changed components.


12. Write a command to check the status of the modified files after adding

git status

This time all the files names will come in green color.


13. Next step is to commit the changes to git. Run command: 

git commit -m "adding changes part of SF story"


14.  At the end we need to push the code. Run Command:

git push

Monday, June 12, 2023

How to Use Custom Label in LWC

In Lightning Web Components (LWC), you can use Custom Labels to store and manage text values that are displayed in your component's UI. Custom Labels allow you to define text values once and then reference them throughout your code, making it easier to maintain and update the text values as needed.

Here are the steps to use Custom Labels in an LWC:

First, create a Custom Label in your Salesforce org. Go to Setup > Custom Labels and click the "New Custom Label" button. Give the label a name, enter the text value you want to store, and click "Save" to create the Custom Label.

In your LWC, import the Custom Label using @salesforce/c.label syntax, and then use it as needed. For example, if you have a Custom Label called "My_Label", you can import it in your LWC as follows:

import MY_LABEL from '@salesforce/label/c.My_Label';

Once you have imported the Custom Label, you can use it in your component's template or JavaScript code. For example, if you want to display the Custom Label in your component's template, you can use the following code.

HTML File:

<template>
    <lightning-card  title={myLabel.CLabel} variant="narrow">
        <p>
            <lightning-button label={myLabel.BLabel}>
            </lightning-button>
        </p>
        <p>{myLabel.ALabel}</p>
    </lightning-card>
</template>

Javascript File:

import { LightningElement } from 'lwc';
// importing Custom Label
import ALabel from '@salesforce/label/c.C1Label';
import BLabel from '@salesforce/label/c.C2Label';
import CLabel from '@salesforce/label/c.C3Label';
export default class CustomLabelExampleLWC extends LightningElement{
    myLabel = {
        ALabel,
        BLabel,
        CLabel
    };
}

In the above code, we imported the Custom Label using the @salesforce/c.label syntax and stored it in variables called ALabel/BLabel/CLabel. We then set that variable to a property called myLabel in the component's JavaScript code, which is then used in the component's template to display the Custom Label's text value.

That's it! You can now use Custom Labels in your LWC to store and manage text values that are displayed in your component's UI.

Tuesday, June 6, 2023

How to Create Map of Accountid and List of Contacts

Here's an example code snippet in Apex that retrieves a map of account IDs and their corresponding list of contacts:

Map<Id, List<Contact>> accountContactsMap = new Map<Id, List<Contact>>();
List<Contact> allContacts = [SELECT Id, AccountId FROM Contact];

for(Contact c : allContacts){   
    if(accountContactsMap.containsKey(c.AccountId)){
       accountContactsMap.get(c.AccountId).add(c);
    } 
    else{
        accountContactsMap.put(c.AccountId, new List<Contact>{c});
    }
}

In this example, we first create a Map that will store Account IDs as keys and lists of associated Contact records as values.

We then query all Contact records and iterate through them using a for loop. For each Contact, we check if the AccountID is already a key in the accountContactsMap using the containsKey() function. If it is, we add the Contact to the existing list of contacts for that account using the add() function. If it's not, we create a new list containing the current Contact and add it to the map using the put() function.

After the loop completes, the accountContactsMap will contain all Account IDs as keys and lists of their associated Contact records as values.

Monday, May 29, 2023

Difference Between Wire Method And Imperative Method in LWC

In Lightning Web Components (LWC), the wire method and imperative method are two different ways of retrieving data from an Apex controller or an external data source.

The wire method is a reactive approach that retrieves data declaratively. It uses the @wire decorator to declare the properties that the component needs and the Apex method to call to get the data. When the component loads or the properties change, the wire method automatically updates the component with the new data.

The imperative method is an imperative approach that retrieves data programmatically. It uses the import statement to import the Apex method and then calls it manually using JavaScript code. This approach is useful when you need to retrieve data based on user input or component events.

Overall, the choice between the wire method and the imperative method depends on the specific use case and requirements of the component. The wire method is more suitable for simple data retrieval scenarios, while the imperative method offers more flexibility for complex data retrieval and update scenarios.

Mostly Viewed