CANNOT EXECUTE FLOW TRIGGER Error With DML Operation in Salesforce

I was developing the Apex code for creating new records using DML(Data Manipulation Language), and when I tried to execute the code, I got the ‘CANNOT EXECUTE FLOW TRIGGER‘ error, stating that we can’t save this record because of the(Flow Name).

In this Salesforce error tutorial, I will explain why the CANNOT EXECUTE FLOW TRIGGER Error With DML Operation in Salesforce occurs and how to resolve this error step by step.

Why Does the CANNOT EXECUTE FLOW TRIGGER Error Occur in Salesforce Apex?

The CANNOT_EXECUTE_FLOW_TRIGGER error in Salesforce occurs when a Flow fails due to an opposing DML (Data Manipulation Language) operation. This usually happens when an Apex trigger or another Flow attempts to update a record that is already being modified within the same transaction, causing a repetitive DML update.

Cannot Execute Flow Trigger in Salesforce [Common Causes and Solutions]

Now, let’s understand the common reasons and solutions for the CANNOT EXECUTE FLOW TRIGGER error occurring in Apex DML.

1. Flow on Same Object

When you try to create or update a record using the DML operation in Salesforce Apex, and if any flow or Apex trigger class is present on the same object, the flow updates a record, which then triggers another flow or trigger to update the same record.

Because of that, a repetitive update occurs when a record update triggers an automation (Flow or Apex Trigger), which in turn causes another update on the same record. If not controlled, this can create an infinite loop, leading to exceeding the governor limit or the CANNOT_EXECUTE_FLOW_TRIGGER error.

CANNOT EXECUTE FLOW TRIGGER Error With DML Operation in Salesforce

For example, we want to update a field using a DML operation, and a flow is also set to update the same field, which again updates the same record, causing a loop.

Solution

Now, let’s understand the solution if we have the flow on the same object for which we are inserting or updating the record.

  • If you are getting this error because of any flow or trigger, you can deactivate the flow on the same object on which you are performing the DML operation, and after completing the operation, you can activate the flow again.
  • In the error, Salesforce also mentions the flow or trigger by which you are getting the error.
  • Also, we can use the Flow Transaction Control, where we can determine before-save flows instead of after-save flows whenever possible. Before-Save Flows execute before the database commit, reducing the chances of recursive updates.

2. Exceeding Governor Limit

Suppose there are multiple records, and you are using separate DML operations for each record or inside the for loop. In that case, the governor limit is exceeded because the limit for DML operations is 150. If the number of DML statements or SOQL queries exceeds the limit, Salesforce throws this error.

For that, you can create a list collection and store the records on which you want to perform DML operations. Then, you can perform operations on that list so that all records will be updated at once.

Solution

In the code below, we first created a list to store the records we wanted to create so that we could insert all those records in a single INSERT operation. After that, we need to create a sObject instance to add values to the field or to create the record. After providing values, we need to add that instance to the list that we created.

If we use a separate DML INSERT operation for each record, then the governor limit will be exceeded, and the above error will occur.

public class Demo {   
    public void UseDML() {               
       List<Contact> contactList = new List<Contact>();

        Contact con1 =  new Contact();
        con1.FirstName = 'First';
        con1.LastName = 'Contact';
        contactList.add (con1);
        
        Contact con2 = new Contact();
        con2.FirstName = 'Second';
        con2.LastName = 'Contact';

        contactList.add (con2);
        INSERT contactList;
    }
} 

Using the List Collection for the DML operations in Apex reduces the chances of exceeding the governor limit in Salesforce.

Conclusion

From the CANNOT EXECUTE FLOW TRIGGER Error With DML Operation in Salesforce, we have analysed why we get this error, and I have explained the solutions to resolve this type of error in Salesforce.

You may like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.