A blog containing Salesforce coding examples in detail.It's a place where you can find solutions to the problems that you may face in your daily salesforce coding. Apex classes,visual force page,we services,Integration,plugins,extensions,Lightning web components,Aura components,Email Messages,Sales cloud,service Cloud,Marketing Cloud.Lightning Web components,salesforce.com, Salesforce Tips and Tricks,short cuts,ApexMocks,Stubs, visibility,Salesforce Shorts
Thursday, June 30, 2022
Wednesday, June 29, 2022
Login History of a salesforce user - Salesforce Globe For You
Solution: There are 2 ways to know the login history of a particular user.
Option 1: Go to the respective user record and then see the login History related list as shown below
Option 2: Use the SOQL query given below to know the login history
select Id, Browser, UserId, LoginTime, SourceIp, Platform,Application,Status from LoginHistory where UserId = '00590000001O70C' order by LoginTime DESC
where UserId will be replaced with the specific user record id.
Saturday, June 25, 2022
count number of times a character appears in a string in apex - Salesforce Globe For You
Solution: use countMatches(substring) to detrmine number of times a substring repeated in a string
Example: To determine number of times character 'e' repeated in 'salesforce globe for you'
String s = 'salesforce globe for you';
Integer numberoftimes = s.countMatches('e');
system.debug('Number of times :'+numberoftimes);
Output:
Note: CountMatches don't work if same character is in uppercase in one place and lowercase in other place.
Convert the string to lower or upper case first and then use the countMatches().
Generate a random 6 digit number in apex - SalesforceGlobe4U
Solution: Use the Math.random() to generate the random number.
Example:
To generate 1 digit random number,use the code below
Integer oneDigitRandomNumber = Integer.valueof((Math.random() * 10));
system.debug('1 Digit Random Number :'+oneDigitRandomNumber);
Output:
To generate 4 digit random number,use the code below
Integer fourDigitRandomNumber = Integer.valueof((Math.random() * 10000));
if(String.valueOf(fourDigitRandomNumber).length() != 4) {
fourDigitRandomNumber = Integer.valueOf(String.valueOf(fourDigitRandomNumber)+'0');
}
system.debug('4 Digit Random Number :'+fourDigitRandomNumber);
Output:
To generate 6 digit random number,use the code below
Integer sixDigitRandomNumber = Integer.valueof((Math.random() * 1000000));
if(String.valueOf(sixDigitRandomNumber).length() != 6) {
sixDigitRandomNumber = Integer.valueOf(String.valueOf(sixDigitRandomNumber)+'0');
}
system.debug('6 Digit Random Number :'+sixDigitRandomNumber);
Output:
Friday, June 24, 2022
How to make picklist field required or mandatory in Salesforce - SalesforceGlobe4U
Solution: If it is a standard picklist field there are 3 ways to make required or mandatory.
Option 1: Making it required from page layout
Option 2: Create a validation rule to fire if the value is empty.
Option 3: Using trigger add error when the picklist value is empty.
https://salesforceglobe4u.blogspot.com/2020/12/how-to-displayshow-error-message-at.html
If the field is a custom picklist field,along with Option 1,Option 2,Option 3,Option 4 also will be there
i.e directly making it required from the field creation itself.
Thursday, June 23, 2022
get the source record from which the current record is cloned from in salesforce apex - SalesforceGlobe4U
Solution: Use the getCloneSourceId() to get the source record from which the current record is cloned from.
Example: In the below piece of code,initially I created one account and then created another account by cloning the first account.
Run the code from the developer console.
Account objAccount = new Account(Name = 'Test Account');
insert objAccount;
system.debug('Account Record Id :'+objAccount.Id);
system.debug('Is this account Cloned? :'+objAccount.isClone());
Account objAccount2 = objAccount.clone();
system.debug('Is this account Cloned? :'+objAccount2.isClone());
insert objAccount2;
system.debug('Account Record Id :'+objAccount2.Id);
system.debug('Source account from which it is cloned :'+objAccount2.getCloneSourceId());
Output:
Check if a record is cloned in salesforce apex - SalesforceGlobe4U
Solution: Use the isClone() to determine if the current record is cloned from something or not
Example: In the below piece of code,initially I created one account and then created another account by cloning the first account.
Run the code from the developer console.
Account objAccount = new Account(Name = 'Test Account Clone');
insert objAccount;
system.debug('Account Record Id :'+objAccount.Id);
system.debug('Is this account Cloned? :'+objAccount.isClone());
Account objAccount2 = objAccount.clone();
system.debug('Is this account Cloned? :'+objAccount2.isClone());
insert objAccount2;
system.debug('Account Record Id :'+objAccount2.Id);
system.debug('Is this account Cloned? :'+objAccount2.isClone());
Output:
Tuesday, June 21, 2022
Login to salesforce - SalesforceGlobe4U
Solution: If you already have a salesforce developer or production account, then
use the below url to login to salesforce
Login to Salesforce (https://login.salesforce.com/)
Enter the username and password and click on login button to login to salesforce.
Note: If you enabled MFA,it will ask for security code to enter or approve notification to proceed.
If you want to login to a salesforce sandbox instance then
use the below url
https://test.salesforce.com/
Next steps will be same as above.
If you want to create a new salesforce developer org to learn or practice, go through the link below
Friday, June 17, 2022
Retrieve tab using package.xml in salesforce - SalesforceGlobe4U
Solution: In this example retrieving the Awards custom object tab.
keep the objectAPI name in members tag and CustomTab in name tag as shown below
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Award__c</members>
<name>CustomTab</name>
</types>
<version>53.0</version>
</Package>
Output:
Wednesday, June 15, 2022
Limit List Iteration(number of records to display from List) in Lightning Web Component - SalesforceGlobe4U
Solution: There are many ways to do it.
1) Filtering the records directly in js file of LWC component
2) Passing parameter to Apex to fetch specified number of elements in query.
In this example we will do the filtering of records in js file.
Note: Custom Label is used to specify number of elements to show.
Example:
limitListIterationInLWC.html:
<template>
<lightning-card title="Limit List Interation in LWC">
<lightning-button label="Show More" data-labelname="more" title="Non-primary action" onclick={fetchRecords} class="slds-m-left_x-small"></lightning-button>
<lightning-button label="Show Less" data-labelname="less" title="Non-primary action" onclick={fetchRecords} class="slds-m-left_x-small"></lightning-button>
<div class="slds-p-around_small">
<template for:each={lstAccount} for:item="account" for:index="index">
<div class="slds-p-left_small" key={account.Id}>
{account.Name}
</div>
</template>
</div>
</lightning-card>
</template>
limitListIterationInLWC.js:
import { LightningElement ,track} from 'lwc';
import getAccounts from '@salesforce/apex/AccountController.getAccounts';
import NumberOfRecords from '@salesforce/label/c.Number_Of_Records_To_Display';
export default class LimitListIterationInLWC extends LightningElement {
@track lstAccount =[];
@track lstTempAccount;
@track noOfRecords = 0;
@track bMore = false;
connectedCallback() {
this.noOfRecords = NumberOfRecords;
getAccounts()
.then(result => {
if(result) {
this.lstTempAccount = result;
result.forEach((acc,index) => {
if(index <= this.noOfRecords) {
this.lstAccount.push(acc);
}
});
}
})
.catch(error => {
this.error = error;
});
}
fetchRecords(event) {
var lab = event.currentTarget.dataset.labelname;
if(lab == 'more') {
this.bMore = true;
} else {
this.bMore = false;
}
if(this.bMore == true) {
this.lstAccount = [];
this.lstTempAccount.forEach((acc) => {
this.lstAccount.push(acc);
});
} else {
this.lstAccount = [];
this.lstTempAccount.forEach((acc,index) => {
if(index <= this.noOfRecords) {
this.lstAccount.push(acc);
}
});
}
}
}
limitListIterationInLWC.js-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>54.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Output:
Wednesday, June 8, 2022
Retrieve assignment rules using package.xml in visualstudio code salesforce - Salesforce Globe For You
Solution: If you want to retrieve all Lead assignment rules use the package.xml given below
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Lead</members>
<name>AssignmentRules</name>
</types>
<version>53.0</version>
</Package>
If you want to retrieve all Case assignment rules use the package.xml given below
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Case</members>
<name>AssignmentRules</name>
</types>
<version>53.0</version>
</Package>
If you want to retrieve all assignment rules use the package.xml given below
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>*</members>
<name>AssignmentRules</name>
</types>
</Package>
If you want to retrieve specific lead assignment rule for example 'Assign To Thomson' rule
use the package.xml given below
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Lead.Assign To Thomson</members>
<name>AssignmentRule</name>
</types>
<version>53.0</version>
</Package>
Output:
Sort picklist field values alphabetically which are shown in object pages in salesforce - SalesforceGlobe4U
Sort picklist field values alphabetically which are shown in object pages in salesforce - SalesforceGlobe4U
Solution: Go to the relevant object --> related picklist field --> go to picklist values section --> click on Reorder button
Now you can rearrange the picklist field values using arrows as shown below
Tuesday, June 7, 2022
Retrieve Accounts which have more contacts(more than 5 contacts) in salesforce - SalesforceGlobe4U
Get Accounts which have more contacts(more than 5 contacts) in salesforce - SalesforceGlobe4U
Solution: Use the SOQL query given below and change the value as needed in having clause.
SELECT Accountid, COUNT(Id)
FROM Contact
GROUP BY AccountId
HAVING COUNT(Id) > 5
Output:
Monday, June 6, 2022
Get Salesforce instance name in apex - SalesforceGlobe4U
Get Salesforce instance name in apex - SalesforceGlobe4U
Solution: Use the SOQL query given below
select InstanceName from Organization
Output:
Saturday, June 4, 2022
use custom label in Lightning Web Component - Salesforce Globe For You
use custom label in Lightning Web Component - Salesforce Globe For You
Solution: first import the label using @salesforce/label/c.labelname and assign to a variable and use that variable in html file
import blogUrl from '@salesforce/label/c.Blog_URL';
customLabelRef = blogUrl;
{customLabelRef}
Example:
customLabelInLWC.html
<template>
<lightning-card title="Custom Label In Lightning Web Component">
Custom Label : {customLabelRef}
</lightning-card>
</template>
customLabelInLWC.js:
import { LightningElement } from 'lwc';
import blogUrl from '@salesforce/label/c.Blog_URL';
export default class CustomLabelInLWC extends LightningElement {
customLabelRef = blogUrl;
}
customLabelInLWC.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__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Output:
Wednesday, June 1, 2022
Use Custom Label in Custom link shown on the record detail page in salesforce - Salesforce Globe For You
Problem: Assume a report is created to show All accounts and the report id is 00O0o00000CGniXEAT as shown in the image below.
Now we need to show this report using custom link in Account object.
In order to make sure custom link should work while moving from one org to other org, we cannot hard code report id directly in custom link.
So we create custom label and store report it in it as shown below.
Solution: Use {!$Label.Account_Report_Id} as shown in the image below.
/{!$Label.Account_Report_Id}
-
How to retrieve validation rules using package.xml in vs code salesforce - Salesforce Globe For You Problem : Assume a validation Mobile_...
-
Assume AccountPage is an apex class in salesforce. Sample Xml file : <?xml version="1.0" encoding="UTF-8"?> &l...