2

I have a record triggered flow in salesforce which performs operations of updating Reports To field in all the Employees of an Organization.

The flow is triggered whenever an Organization ReportsTo field is updated.

It fetches all the employees of that organization and assigns them the Reports To field value inside the loop and stores it into a collection.

After the last item in the loop the collection is passed to update element where the records are updated.

The organisation which is triggering the flow has more than 1000 employees which results in violation of flow governor limits causing the flow to throw an error "Number of Iterations Exceeded".

Below is the flow diagram for your reference:

enter image description here

Please provide a way/solution for handling the governor limits issue in the flow with suitable explanation. Thank you in advance.

Jimesh
  • 479
  • 5
  • 17

2 Answers2

0

This help article contains some suggestions on how to fix this error:

  1. Reduce the total number of records being processed.
  2. Process the records in multiple batches. Keep your own counter using a number variable and increment it every loop, exiting the loop when you're about to hit a limit. Consider adding a screen or wait element before looping through next batch of records.
  3. Check for alternatives using Apex code.

It seems that number 1 and 2 are not really options in your case. Therefore, I'm going to recommend an Apex solution.

I would typically solve this problem with an Apex trigger (on the Organization object) executing in the after context. Depending on the amount of related Employees, I would either:

  • Have a trigger handler class which updates the related Employees, or
  • Execute a batch class which updates the related Employees

Generally speaking, I favor the second option because it's more scalable - it can handle a few or a lot of related records. If you choose the trigger-handler approach and your business grows, you might still have to convert the logic into a batch class in the future.

However, if you do not want to introduce a trigger, you can still have your Flow call an Apex invocable method which, in turn, executes a batch class which updates your related Employees. Either way, you will need some code to achieve your goal.

Here is a sample batch class. You might have to modify it a bit because I don't know the exact object or field names that you need to use:

public class EmployeeUpdaterBatch implements Database.Batchable<sObject> {

    // Invocable method which executes the batch class
    @InvocableMethod
    public static void invokeEmployeeUpdater(List<Organization> organizations) {
        EmployeeUpdaterBatch employeeUpdateBatch = new EmployeeUpdaterBatch(
            organizations);
        Database.executeBatch(employeeUpdateBatch, 200);
    }

    // Batch class
    private List<Id> organizationIds;

    public EmployeeUpdaterBatch(List<Organization> organizations) {
        this.organizationIds = getIdsFromRecordList(organizations);
    }

    private List<Id> getIdsFromRecordList(List<sObject> records) {
        return new List<Id>(new Map<Id, sObject>(records).keySet());
    }

    public Database.QueryLocator start(Database.BatchableContext bc) {
        String query = 'SELECT Id, Organization.ReportsToId FROM Employee '
            + 'WHERE OrganizationId IN :organizationIds';
        return Database.getQueryLocator(query);
    }

    public void execute(Database.BatchableContext bc, List<Employee> scope) {
        for (Employee employee : scope) {
            if (employee.Organization.ReportsToId != null) {
                employee.ReportsToId = employee.Organization.ReportsToId;
            }
        }

        update scope;
    }

    public void finish(Database.BatchableContext bc) {
        // Notify that the process completed - maybe send an email
    }
}
Arepa Slayer
  • 443
  • 1
  • 7
  • 20
0

If your flow is an Autolaunched flow, then it’s a simple choice. Use the Pause element, working like magic and configure it to resume your flow after 0 hours of current date-time. Please note that pausing the flow makes the rest of the flow run asynchronously in a separate transaction.

https://forcepanda.wordpress.com/2020/02/04/how-to-cheat-flow-limits/comment-page-1/?unapproved=12451&moderation-hash=5450b3bb955c04592e053c4470232d5a#comment-12451