Winter ’21 Apex New Feature

 

This post will cover some of my favorite Apex new features from Winter ’21. I have published another post that covered new existing enhancements for Flow Designer. To explore all the new features from Winter ’21 release, I highly recommend –
  • Sign up to a pre-release org and explore all the new features.
  • Post your findings in the Trailblazer Community and include the hashtag #Winter21Treasure to receive Trailhead Treasure Hunter badge
Here is the list

Safe Navigation Operator

With the introduction of Safe Navigation operator (?.), we are no more needed to perform an explicit null check to avoid NullPointer Exception. Basically, if the left side expression of the operator evaluates to null, then the right side will not be evaluated. 
//Before Winter’21
List<Contact> allContacts = [
SELECT Id
FROM Contact
WHERE name = :contactName
];
if (allContacts.size() > 0) {
String Id = allContacts.get(0).Id;
}

//After Winter’21
String Id = [SELECT Id FROM Contact WHERE name = :contactName]?.get(0).Id;

Update Resources with the PATCH HTTP Method in Apex Callouts (more…)

Winter ’21 Flow Enhancements

 

Winter ’21 pre-release org is ready. And without any surprise, Winter ’21 is also bringing lots of great enhancements. In this post, I am going to focus on a few Flow Enhancements that we will be getting soon and will be able to take advantage of. 

Handling Deletion Through Record Triggered Flow

With Winter ’21, we will be able to trigger a flow when the record is getting deleted. This is similar to Delete context from Trigger. To me, this is a great enhancement. Few things to note, you can. only do Before the record is deleted, not after the record is deleted. And if the deleted record is recovered later, that will not rollback any changes/actions performed as part of the flow. To handle that situation, the recommended approach is to write Apex after undelete trigger.

Auto Layouts (more…)

Show Your Data in Datatable, Map or Tile Format Inside Flow

In this post, I am going to show you one of the most powerful AppExchange flow solutions built by Salesforce Labs. If you ever needed to display a collection of records in some format, mostly in datatable format to the user and provide options to the user to select records from that list and finally perform some actions based on those selected records, then this AppExchange pre-built flow solution is your going to be your friend. 😃

Check out my youtube channel for more Salesforce Videos here.
All about Salesforce Release – Spring 21 here

The name of the solution is Flow Datagrid Pack. With this solution, you have the power now to display your collection of records in three different formats –

  • Data Grid – Basically displays the records in a multi-select datatable.
  • Tile Grid – Displays in multi-select (configurable) tiles format.
  • Map Grid – Displays multi-select markers on map where you need to display records on the map. Ex: Display a list of accounts in Ontario within the map.
Let’s configure this flow solution for a basic use case.

Use Case:

Need a solution where all the contacts from an account will be displayed in datatable and the user can select from the list. Based on the selected contacts, only those contacts should be displayed on the next screen.

Solution – Flow:

I build the below flow using the Flow Datagrid pack.
Here are the steps –

Fetch Associated Contacts

In this step, I am fetching all the contacts associated with an account.

Loop Through All Contacts and Store Contact Ids

Here I am looping through all the contacts and storing their ids in a collection variable. This step is needed as a collection variable with record ids are needed in the Flow Datagrid Pack to display the records. 
Note – I am not sure why this extra loop is required, but my initial thought is that this loop can easily be removed and Flow Datagrid Pack should work on the collection of records.

Display All Contacts in Datatable

Here I am using the Flow Datagrid Pack pack as displayed below.
To start with, I am just using the basic input parameters, but you can configure a lot. The details level of the configuration details, you can get to know from here.
  • ID List: Here I am using the collection variable I have created in an earlier screen which stores all the contact ids.
  • Object API Name: I am using Contact here as object name. You can literally work on any standard or custom objects. Note – With External objects, this solution is not working as of today.
  • Field API Names: I am using the API names of the fields which I would like to display inside the datatable.

Display Selected Contacts

In this step, I am displaying the selected records using the same Flow Datagrid Pack. 
The difference is that here in the ID list I am using the auto-generated collection variable from the previous screen which stores the id of only selected contacts.
This is how the flow will look like –
That’s all!! Isn’t it super easy? I highly recommend everyone to try this one. 
Note – To get all the pre-built cool Flow solution check the dedicated flow section in AppExchange. Link

Spring’20 Brings Improved Code Coverage Result for Apex Test Class

 

Writing test classes to cover both positive, negative, and bulkified scenarios are always the best practices that each developer should follow. While writing test classes, our approach should be to cover as much as code and all the scenarios. There is a restriction from Salesforce that if there is not enough coverage (75%) for the code, then Salesforce will not allow you to deploy your code to production.

So identifying the code coverage is very important. Now there are multiple ways we can get to know the code coverage like – Setup menu, Developer Console, SOQL Query, Salesforce CLI, or Salesforce extension in Visual Studio Code. With so many options there comes the difficulties. Each option presents the code coverage in a different way and more importantly, each option calculates code coverage in a different way. This always creates confusion. For example, when a developer writes the test class and executes that from CLI, it provides the code coverage, let’s say 90%, which should be good enough to deploy the code into Production. But while deployment, the code coverage came down to 50%, and thus deployment stops. The reason for this drop in code coverage is that while deploying code coverage is calculated at the org level i.e. percentage of coverage for that class across the org.

To solve this problem, Salesforce comes up with the Enhanced Code Coverage option. With this option, developer will get – 

  • Different classes being covered by the test class. The percentage of each class covered and uncovered lines from each class.
  • Test result which will pass/fail result for each test method.
  • Finally Test Summary will show “Test Run Coverage” and “Org Wide Coverage”.
This change was introduced in Spring 20 release. You can read more about it – https://releasenotes.docs.salesforce.com/en-us/spring20/release-notes/rn_sf_cli_code_coverage_apextests.htm
In order to use this feature in Salesforce CLI, the first thing you need to do is set up your environment variable.

Setting up the environment variable

There are three ways to do that. 
You can do that for a single command by doing 

SFDX_IMPROVED_CODE_COVERAGE=true sfdx force:apex:test:run ...
or set it for a shell session with

export SFDX_IMPROVED_CODE_COVERAGE=true

or if you want to set it permanently, then set it in the config file for your shell/command prompt.
Setting up the environment variable is important as it will eliminate Developer Console and VS Code/CLI code coverage mismatch.

Running the test

Once the environment variable is set up, you can run your test by –

sfdx force:apex:test:run -n “CommunicationHandlerTest” -c -r human

Here I am trying to execute the test class – CommunicationHandlerTest. To know more about the options, read – https://github.com/forcedotcom/salesforcedx-vscode/issues/2224
The result will be something like this 
I highly recommend everyone to use this new feature. Thank you.

Spring’20 brings one Invocable method for all

 

Have you ever asked this question to yourself – Why I have to write multiple invocable methods for the same functionality working on different objects? Definitely not a very clean approach. Thankfully Salesforce came up with the enhanced version of Invocable methods which now can work on multiple objects doing similar types of work. 

With this new enhancement, you can now write Invocable methods which will accept generic sObject data type. As a result, you will write one single Invocable method which will now work for multiple sObjects. The advantage is that if in your org, you have multiple Invocable methods doing similar operations (you just created them because they need to work on different sObjects), it the time to merge them to make a single Invocable method and make your org look clean.

In this post, I am going to show the example of creating one Invocable method which will be used to create Lead and Account when calling from different flows.

Here is the Invocable method:

public class createRecords {
@InvocableMethod(
label = ‘Create Records’
description = ‘Update records or create new records’)
public static List<myResponse> createRecords(List<myRequest> request){
List<SObject> records = new List<SObject>();
for(myRequest singleRequest : request){
records.addAll(singleRequest.records);
}
List<myResponse> response = new List<myResponse>();
myResponse singleResponse = new myResponse();
Schema.SObjectType sObjectType = records.get(0).getSObjectType();
if(sObjectType != null){
String listType = ‘List<‘+sObjectType+‘>’;
List<SObject> castRecords = (List<SObject>)Type.forName(listType).newInstance();
castRecords.addAll(records);
upsert castRecords;
singleResponse.records = castRecords;
response.add(singleResponse);
}
return response;
}
public class myRequest {
@InvocableVariable(label = ‘Records to create’)
public List<SObject> records;
}

public class myResponse {
@InvocableVariable(label = ‘Inserted/Upserted records’)
public List<SObject> records;
}
}

As you can see, this above Apex code is written in such a way that it can work on any sObjects. In the actual Invocable method, we are typecasting the input records (myRequests -> records) to the actual sObject.

Below are two flows from where I am going to call this Invocable method passing the type of sObject against which the function needs to work.

Account Create Flow

Lead Create Flow

As you can see in both the flows, I have used the same Invocable method, just method the sObject name against which it will operate. I think this enhancement is going to make development very clean, reducing the surface area od code maintenance and increasing code reusability.