DML (data manipulation language) is a computer programming language which is used for manipulating data in a database. In Salesforce, DML operations can be used to insert, update, merge, delete, and restore records. They play a key role both in Apex and Flow.
DML operations can be performed both on a single sObject and a list of sObjects. There is a governor limit of 150 DML statements per transaction. Therefore, it is recommended to perform bulk DML operations using a list of records. Complete this Trailhead module to learn more about DML operations. You can read this post to learn how to work with collections in flow.
DML Operations in Flow
Create Records, Update Records, and Delete Records are the three DML operations that you can perform using Salesforce Flow. These are the standard data elements of flow. However, the remaining DML operations can also be performed using an Apex code in flow. Read this post to learn more about the available elements in the Toolbox of Salesforce Flow.
You can use these elements to create, update, or delete records in a variable. It is also possible to update or delete records according to a condition. However, in some cases, there might be no records in your variable (it can be a single or collection variable) or no records that meet your criteria.
Let's see what happens if there are no records.
Create an Empty List of Records
Let's say that you have a single record variable for the Account object. At one point of the flow, you want to create this record, but for some reason, it is empty. As you can see, flow doesn't fail, it just doesn't create the record.
It has the same behavior with an empty record collection variable.
As you can see, the result is the same. No records were created.
It is important to note that even though it does not create any records, it consumes from DML statement governor limits. Because this limit is about statements and not the successful operations. Consequently, it might be the best practice to add a Decision element before performing this DML operation.
Update an Empty List of Records
Updating a single record or a collection of records has the same behavior as the create DML operation.
Let's update all the accounts that meet a condition like shown below.
What will happen if there are no accounts that meet this criteria? Nothing. As you can see, the flow doesn't fail, it just doesn't update any records.
Unlike the create operation, it does not consume from the DML limits if there are no records to update.
Delete an Empty List of Records
Unlike creating and updating an empty list of records, the delete DML operation actually needs a non-empty variable.
As it is also written in the Delete Records element, you have to make sure that there is a record to delete. Otherwise, flow can't find the record to delete and it fails.
As you can see, flow failed because it couldn't find any records to delete.
How to Prevent Getting This Error
Since Delete Records operation needs a record, you have to make sure that there is at least one record to delete. In order to do so, you have to put a decision before performing the delete DML operation. There can be two options.
1. If you are already using a record variable or a record collection, add a Decision element to check if it is null.
As you can see in the picture below, flow doesn't try to delete the records since there are no records to delete.
2. If you are using the Delete Records element to delete records that meet a criteria, first you have to add a Get Records element to find those records. After getting these records, use a Decision element to check if they exist.
Let's say that you want to delete the accounts that meet this criteria.
If there are no accounts that meet this criteria, then flow will fail at this step. To avoid this, add a Get Records element to find the accounts.
After getting the account records, add a Decision element to check if they exist.
As you can see, since the flow failed to find any records, it didn't try to delete them.
To sum up, before trying to delete records in flow, you have to make sure that there are records to delete. However, it is not required to do so for create and update DML operations.
Leave a Reply