SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Tuesday 6 February 2024

Repeater Component (Beta) in Salesforce Flow | Spring'24 Release

Hello Trailblazers,

In this post we're going to learn about new Repeater component provided by salesforce for screen flows. Let's begin!

Screen Flow: Create contacts for Account

Let's start with a basic use case: We want to create multiple contacts for our account record in one go. The standard screen flow approach for this will be: Show contact fields (contact form) on a screen, the user can fill those, maybe have a checkbox at the end which says, do you want to create another contact? If the user checks it, reload the same screen again on clicking next button so that the user can fill details for another contact. If the user doesn't check that checkbox, create all the contact records for which the user entered information till now.

The drawback with this approach is: User cannot view the details of all the contacts on a single screen while editing. Due to this, he/she may go back and forth to update a particular contact which may lead to some other issues like: duplicate records (as you may be adding contact record to the list as well side by side - in the same loop which is used to repeat screen, with a plan that you can insert those at the end - give this a try) or there maybe some other issues too.

We can solve these problems using the new Repeater component provided by salesforce for screen flows. Please note that this component is presently in beta.

Creating the base flow with empty screen

1. Go to Setup and search for Flows. Click on the New Flow button to create a new flow
2. On the New Flow screen, choose Screen Flow and click on Create button
3. Click on the + icon between Start and End element and choose Screen to add a screen element to our flow
4. Add a label for the screen as Create Contacts for Account, the API name will auto populate to Create_Contacts_for_Account. Click on Done
5. Before proceeding forward, let's save our flow. Click on the Save button at the top right corner. In the Save the flow popup, populate the Flow Label as Create Contacts for Account, the Flow API Name will autopopulate as Create_Contacts_for_Account. Click the Save button in the popup screen to save the flow.
6. Your flow will look as shown below

Populate the screen with repeater component with contact fields inside it

Open the screen element and add repeater component to it as shown below:
The API name for the same is: ContactDetailsRepeater. Now, let's add some contact fields in our repeater component.

Let's add a text field as shown below, the Label of the field can be: First Name with the API Name as First_Name.
Similarly, let's add the last name field as well with Label as Last Name and API Name as Last_Name. We'll also make this field Required by checking the Require checkbox as shown below:
Click on Done.

Iterate contacts to create a contact list

The repeater component will provide us a list of contact records. Let's iterate that list by using a loop. Click on the + icon again and add a loop element as shown below:
Loop Label can be Iterate Contacts and API name can be Iterate_Contacts. For the Collection Variable you can have it as {!ContactDetailsRepeater.AllItems} as shown in the below images:

The final screen will look like this:
Now, let's add an assignment element inside the loop to create our contact record. Click on the circle (+ element) inside the loop and choose assignment as shown below:
Our assignment Label can be Create Contact Record and API name will be Create_Contact_Record as shown below:
Now, to create a new contact and add it to the list, we need some resources. Let's create a new resource named Contact which will store the contact record as shown below:
Resource Type: Variable
API Name: Contact
Data Type: Record
Object: Contact

Click on Done and create another resource to store list of contacts as shown below:
Resource Type: Variable
API Name: ContactList
Data Type: Record
Object: Contact
Allow multiple values (collection): True

Click on Done. We also want to associate our contacts to the account record. Let's create another resource which will store the id of the account as shown below:
Resource Type: Variable
API Name: AccountId
Data Type: Text
Availability Outside the Flow: Available for input

Click on Done and let's update our assignment element as shown below:
{!Contact.FirstName} Equals {!Iterate_Contacts.First_Name}
{!Contact.LastName} Equals {!Iterate_Contacts.Last_Name}
{!Contact.AccountId} Equals {!AccountId}
{!ContactList} Add {!Contact}

We assigned the first name and last name values from current item of the list provided by repeater to the first name and last name of the contact record. Then we assigned the value of AccountId in our contact record to the value of AccountId variable. Finally, we added our contact record to the ContactList.

Inserting Contacts in Salesforce

Now, we need to insert this list, click on the circle (+ element) outside the loop and choose Create Records element as shown below:
In the New Create Records section, add the label as Create Contacts, the API name as: Create_Contacts. The value for How Many Records to Create should be Multiple and in the Record Collection field, we can choose our ContactList collection as shown below:
Now, we can save and activate the flow. Let's go to our account record's detail page and embed the flow there.

Adding Screen Flow to Lightning Record Page

Go to any account record in salesforce. Click on Gear icon and choose Edit Page.
In the Lightning App Builder, search for Flow in the components section in the left, drag and drop it anywhere on the page you like. In my case, I've added it to the right. Choose our Create Contacts for Account flow and for the AccountId variable, check the Pass record ID to this variable checkbox as shown below:
Click on Save. Activate the page if needed and go back. You'll notice that the flow is present on the page as shown below:

Demo

Our changes are done. Let's take a look at the demo:
As you can see above, as we click on the + Add button, the section inside the repeater component is repeated so that we can create multiple contacts in one go. Similarly, we can also click the Remove button to remove a section. As we clicked Next button, 2 contacts: Richard Hendricks and Erlich Bachman were created and attached to the current account record.

Note: The Repeater component is currently in Beta and supports these components inside it as provided by salesforce: Checkbox, Checkbox Group, Currency, Date, Date & Time, Long Text Area, Multi-Select Picklist, Number, Password, Picklist, Radio Buttons, Text, and Display Text

That's all for this tutorial everyone, I hope you liked it, let me know your feedback in the comments down below.

Happy Trailblazing!!

Wednesday 31 January 2024

Display calculated information on the same screen in screen flow | Use Display Text as Reactive Component | Spring'24 Release

Hello Trailblazers,

Salesforce recently allowed some more components to react to changes happenning on the same screen in a screen flow. In this post, we're going to take a look at an example to understand this update.

Screen Flow: Time Log

Let's say you're working on an HR Application created in salesforce. You want your company's employees to add/update their weekly time log on a screen. Let's create a screen flow for the same.

Creating the base flow with empty screen

1. Go to Setup and search for Flows. Click on the New Flow button to create a new flow
2. On the New Flow screen, choose Screen Flow and click on Create button
3. Click on the + icon between Start and End element and choose Screen to add a screen element to our flow
4. Add a label for the screen as Time Log, the API name will auto populate to Time_Log. Click on Done
5. Before proceeding forward, let's save our flow. Click on the Save button at the top right corner. In the Save the flow popup, populate the Flow Label as Time Log, the Flow API Name will autopopulate as Time_Log. Click the Save button in the popup screen to save the flow.
6. Your flow will look as shown below

Populating the flow screen with input fields and reactive elements

Now, it's time to populate our flow with some input fields. Let's edit our Time Log screen element and add some fields there as shown below:

As you can see above, we added 5 number fields to fill the time log for each day. The labels and API names for the same are: Monday, Tuesday, Wednesday, Thursday and Friday. Now, we're going to add a display text which will be reactive as it'll refer to the number fields we created above. The display text element configuration is provided below:
This Display Text is showing us the values entered in all the 5 fields above. It's name is ReactiveDisplayText and the value is provided below:
Hours worked on Monday = {!Monday}
Hours worked on Tuesday = {!Tuesday}
Hours worked on Wednesday = {!Wednesday}
Hours worked on Thursday = {!Thursday}
Hours worked on Friday = {!Friday}
Total Hours =
If you notice above, for Total Hours, we haven't referred to any resource yet. This is because we don't have one for this. Click on Done and let's add another resource in our flow named Total which will consist of the sum of all the hours entered in these fields. The details are provided below:
If you notice above, we clicked the New Resource button and created a new resource named Total with the details provided below:

Resource Type: Formula
API Name: Total
Description: This formula will store the total hours, employee has worked in this week
Data Type: Number
Decimal Places: 0
Formula: NULLVALUE({!Monday},0) + NULLVALUE({!Tuesday},0) + NULLVALUE({!Wednesday},0) + NULLVALUE({!Thursday},0) + NULLVALUE({!Friday},0)

In this formula, we're adding values of all the fields: Monday, Tuesday, Wednesday, Thursday and Friday. We're wrapping each field value in NULLVALUE(), so that - in case, the value of any field is NULL (or let's say we haven't filled the field yet), it will be replaced by 0 in our formula calculation.

Click on Done.

Now, let's get back to our screen and refer to the Total variable there as shown below:
We placed our cursor after "Total Hours =" text, searched for Total in our Resource Picker and clicked on this option as it appears. The updated value for our display text is shown below:
As you can see above, the Total Hours now refers to the {!Total} resource that we created. Click on Done. I'm sharing the final value of display text again below for your reference:
Hours worked on Monday = {!Monday}
Hours worked on Tuesday = {!Tuesday}
Hours worked on Wednesday = {!Wednesday}
Hours worked on Thursday = {!Thursday}
Hours worked on Friday = {!Friday}
Total Hours = {!Total}
Let's debug our flow to see how it's working!

Flow Demo

Save this flow using the Save button and click on the Debug button to debug our flow:
This is how our flow is working now:
If you notice, the values we're entering are reflected in real time in our display text component and the total is also updated properly. This is how you can use Display Text as a reactive component in a screen flow.

Using text template to show reactive changes

Do you know, in the latest release, Text Template resources can also respond in real time to changes in the output of a component. Let's create a reactive text template to see how this works using an interesting example but before this, we need to create two static text templates. One will show the success message and another one will show the warning message.

Let's create a success message text template i.e. a new resource with the details as provided below:
Resource Type: Text Template
API Name: SuccessText
Description: This message will be shown when the total time log value for the week is greater than or equal to 40
BodyYour time log looks great. Thank you!

If you notice above, the text in the body of this text template is bold and is in Green color. Similarly, we can create another text template for the warning message. The details are provided below:
Resource Type: Text Template
API Name: WarningText
Description: This message will be shown when the total time log value for the week is less than 40
BodyNote: You've worked less hours in this week

Again, here as well, we've kept the text in the body of this text template as bold and it's in Red color. Click on Done.

Now, let's create a formula resource which we'll refer in our reactive text template. The details are provided below:
Resource Type: Formula
API Name: SuccessWarningText
Description: This formula will provide the warning message if the total hours are less than 40, otherwise it'll show the success message
Data Type: Text
Formula: IF({!Total} < 40, {!WarningText}, {!SuccessText})

This formula will basically return the warning text template if the total hours added by the user are less than 40 for the current week. Otherwise, it'll return the success text template. Let's create another text template now, which will refer to this formula and will therefore become reactive according to the input added by the user. The details for the same are provided below:
Resource Type: Text Template
API Name: ReactiveTextTemplate
Description: Text template used in the screen
Body: {!SuccessWarningText}

If you see, this text template is referring to our SuccessWarningText formula which we created before. It'll show the success or warning message on the screen according to the input added by the user. Click on Done.

Now, it's time to edit our screen and add another Display Text element as shown below:
The API name for this display text is ReactiveTextTemplateDisplayText and it's referring to our {!ReactiveTextTemplate} variable as shown above. Click on Done. Save and debug the flow again, the updated flow is working as shown below:
So, this is how you can also use text template as a reactive component to show real time output for the input entered by the user.

That's all for this tutorial everyone, I hope you liked it. Let me know your feedback in the comments down below.

Happy Trailblazing!!

Monday 22 January 2024

Solution for "System.QueryException: List has no rows for assignment to SObject" - Null Coalescing Operator (??) | Spring'24 Release

Hello Trailblazers,

According to the International Survey of Issues faced by Salesforce Developers (which was NEVER conducted) by SFDC Stop: Every salesforce developer has faced the below error at least once in their developer career:

System.QueryException: List has no rows for assignment to SObject

This error mainly occurs when you're trying to perform a SOQL query which (ideally) should return a single record and you're trying to store that record in a variable of that sObject type which you're querying. Let's take a look at the below code as an example:
Contact contactRecord = [SELECT Name FROM Contact WHERE Id = '003H3000001lIHsIAM'];
System.debug(contactRecord);
If you notice above, we're querying a contact record using the id of the contact and then displaying it in the log. The result for this code snippet is shown below:
A contact with name Andy Young is queried and assigned to our contactRecord variable successfully. I checked it in our salesforce org, and the contact with the same id and name as Andy Young is present as shown below:
Till now, everything is working as expected. Now the question is: What will happen when we delete this contact record and try to execute the same code again?

Let's give this a try! I deleted this contact record from our salesforce org and now we are getting the below error when we try to open the detail page of this contact:
The requested resource (our contact record) does not exist (in our salesforce org), because we deleted this record. I executed the same code snippet to query this contact record and this time, we're getting the error "System.QueryException: List has no rows for assignment to SObject" as you can see below:
This error is coming because our contact record with id 003H3000001lIHsIAM doesn't exist in our salesforce org now. The query which we're performing is returning a list which has no rows (no records) to assign to the contactRecord variable which is of type Contact sObject. So, we can say "List has no rows for assignment to SObject".

In order to solve this error, we can use the Null Coalescing Operator (??) which is delivered by salesforce in Spring 24 release. This operator is denoted by two question marks: ?? and is a binary operator of form: x ?? y. It basically says that: If x is not null, it'll return x. Otherwise, it'll return y. Now, for our scenario, the list of contact records we're trying to query is coming as empty which is why we cannot refer to the first element of that list in order to assign it to contactRecord variable. We can modify our code to assign a default contact record when the list is coming as null. Let's see the modified code below:
Contact contactRecord = [SELECT Id FROM Contact WHERE Id = '003H3000001lIHsIAM'] ?? new Contact(LastName='Malhotra');
System.debug(contactRecord);
If you see above, at the end of the query, we've added: ?? new Contact(LastName='Malhotra');

We're using the Null Coalescing Operator ?? at the end of our contact query, keeping a new (default) instance of contact object where LastName = Malhotra. Let's see what happens when we try to execute this code:
As you can see above, instead of throwing error, our debug is now showing the default contact record with last name as Malhotra. This means that: the instance of our contact record which is after the Null Coalescing Operator is assigned to our contactRecord variable. This is because the list of contact records which were queried was coming out to be null.

Therefore, using our new Null Coalescing Operator, we're able to deal with the scenario when our query doesn't return any rows so that we don't get any error.

Points to remember about Null Coalescing Operator

  • Operator precedence will apply as is while using Null Coalescing Operator. For example: a ?? 25 + 50 ?? b will be evaluated as: a ?? (25 + 50 ?? b) and not (a ?? 25) + (50 ?? b). We should use appropriate paranthesis to make sure the evaluation is done correctly. Let's take a look at the result of mentioned examples below:
    As we executed the above code snippet, you can see that the result for a ?? 25 + 50 ?? b is coming out to be 10 as a has the value 10. However, the result for (a ?? 25) + (50 ?? b) is coming out to be 60 as the evaluation for this will be 10 + 50 = 60.

  • You cannot use Null Coalescing operator as the left side of the assignment. For example: a ?? b = 50; doesn't evaluate to anything as shown below:
That's all for this tutorial. I hope you liked it, let me know your feedback in the comments down below.

Happy Trailblazing!!