Category: Apex

10 Apex Programming Best Practices

Apex is a strongly-typed, object-oriented programming language that allows developers to execute flow and transaction control statements on the Lightning Platform. It is a key component of the Lightning Platform and allows developers to build robust business logic and integrations. There are a number of best practices that Apex developers should follow to ensure their code is efficient, maintainable, and scalable. Below are ten tips for writing high-quality Apex code. You can read – Lightning Web Component Best Practices Salesforce Flow Best Practices 10 Apex Programming Best Practices Use of "sObject" Data Type Use the “sObject” data type when...

Read More

How to Convert Leads to a Person and Business Account at the Same Time | Spring 21 New Feature

 If in your Salesforce Implementation, you are dealing with both individual and group accounts, then with the release of Spring 21, now you can link your converted leads into both individual and group accounts with the new LeadConvert Methods.Note This feature is only available in Enterprise, Performance, and Unlimted Editions.VideoIf you prefer video, here is the youtube video MethodsThe new LeadConvert methods will allow you to link converted leads into a business and person account instead of a contact. Below are the methods available -getRelatedPersonAccountId() – Gets the Id of an existing person account to convert the lead into.setRelatedPersonAccountId(String personAccountId) – Sets the Id of an existing person accoung to convert the lead into.getRelatedPersonAccountRecord() – Gets the entity record of a new person account to convert the lead into.setRelatedPersonAccountRecord(Entity relatedPersonAccountRecord) – Sets the entity record of a new person account to convert the lead into.LeadConvertResult class holds the result of the lead conversion.Example 1The below example uses the existing personAccountId to convert the lead into. After the execution of this method, lead with id leadId will be converted into both business account with id businessAccountId and person account with id personAccountId.public static void convertLeadWithExistingPersonAccount(String leadId,     String businessAccountId, String personAccountId, String opportunityId) { Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(leadId); lc.setAccountId(businessAccountId); //New Method to use the existing person account id to convert the lead into. lc.setRelatedPersonAccountId(personAccountId); lc.setOpportunityId(opportunityId); lc.setSendNotificationEmail(false); LeadStatus convertStatus = [SELECT                   ...

Read More

Top Apex Features From Spring 21

Spring 21 is bringing a couple of Apex features that can be definitely helpful. This post will cover a few of my favorite Apex features from Spring 21.You can find all my Spring 21 release posts here.Feature #1With Spring 21, we can fetch the entire custom metadata records or a single record from the custom metadata. So no more SOQL query to fetch custom metadata records. Here is the video where I have explained the feature. With getAll() method, now we can get the entire custom metadata records. Here is the small code snippetpublic static void fetchAllMetadata() {    Map<String, Country__mdt> allCountries = Country__mdt.getAll();    System.Debug(‘allCountries Size: ‘ + allCountries.size());    for(String countrName : allCountries.keySet()){        System.Debug(‘Country Name: ‘ + countryName + ‘ Capital: ‘             + allCountries.get(countrName).Capital__c);    }}With getInstance( recordId ),  getInstance( qualifiedApiName ), getInstance( developerName ) method, now we can get the single custom metadata record. Here is the small code snippetpublic static void fetchCapital(String countryName) {    Country__mdt singleCountry = Country__mdt.getInstance(countryName);    System.Debug(‘Country Name: ‘ + countryName + ‘ Capital: ‘         + singleCountry?.Capital__c);}Feature #2With Spring 21, we can now convert 18-character Id to 15-character Id. Here is the video where I have explained the feature. With to15() method, now we can get the 15-character Id of the corresponding 18-character Id. Here is the small code snippet.public static void convertId(String id_18) {    ID test18 = id_18;    System.Debug(’15 Digit ID: ‘...

Read More

How to use Safe Navigation Operator (?.) in Apex | Winter 21 New Feature

 Salesforce rolled out a new feature in Winter 21 release, it’s called – SAFE NAVIGATION. Safe navigation operator is a very useful syntax for Salesforce Developers and it will make the coding clean. The main purpose of this syntax is to avoid null pointer exceptions.Basic SyntaxString accountName = accountRecord?.Name;The above statement will store accountRecord.Name value in the variable accountName. As you can understand that accountRecord is basically the sObject variable holding Account record and then we are using that variable to fetch the Name property. Now, what will happen if the accountRecord is null, then null.Name will definitely give the null pointer exception, right? But the answer is No. Because we have used the safe navigation operator(?.) here. Safe navigation operator will make the check and if the accountRecord is not null, then only store the Name field to accountName, otherwise, it will just store null there.How it works:If the left side of the chain expression (?.) evaluates to null, then the right side will not be evaluated. This safe navigation can be used in the method, variable, and property chaining. The part of the expression that is not evaluated can include variable and property chaining.Example 1:Let’s say we have a method that will take the AccountNumber as a parameter and print the AccountName.Before Winter 21 without Safe Navigation, our code will look like –    public static void getAccountNameWithoutSafeNavigation(String accountNumber){...

Read More