Apex beginner here so I'm open to any and all suggestions. I'm working on a flow + invocable apex combination to mass reassign records. Basically trying to take variables obtained by the flow and pass them along to apex. I have the flow querying and storing a couple thousand account records in a record collection and then apex is updating this collection of accounts with a new owner. It's working when a small number of records need to be reassigned, but I get the following error when trying to update a couple of thousand records - "An Apex error occurred: System.LimitException: Apex CPU time limit exceeded". I'm looking to see where I'm going wrong. I believe it has to do with the nested loop and I understand a Map might be my solution, but I'm unsure exactly how to make the adjustment.
Here's the class with the Invocable Variables that are getting set via the flow:
public class TerritoryAssignmentVariables {
@InvocableVariable
public List<Account> accounts;
@InvocableVariable
public String newOwnerId;}
And here's another class I've setup for updating the records based on the above variables:
public class TerritoryAssignmentClass {
@InvocableMethod(label = 'Update Records')
public static void updateAccounts(List<TerritoryAssignmentVariables> oldValues){
List<Account> Updatedaccounts = new List<Account>();
for(TerritoryAssignmentVariables value : oldValues){
for(Account acc : value.accounts){
acc.OwnerId = value.newOwnerId;
Updatedaccounts.add(acc);
}
}
update Updatedaccounts;
}
}
Does anyone have any tips or suggestions on this? I'd appreciate any assistance!
Account
? Examining debug log would give you a clue