This help article contains some suggestions on how to fix this error:
- Reduce the total number of records being processed.
- 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.
- 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
}
}