tallpacker
tallpacker

Reputation: 21

A flow looping through 2000 records throws "APEX CPU time limit exceeded" error. Any fixes in flow?

I have a requirement to create a flow to walk through the campaign members of a campaign, and attach an activity at each member's contact record to indicate that the campaign email was sent.

The logic is simple: Fetch the members (a single SELECT returning a collection), loop through the collection, and create a copy of an activity with WhoID = Contact ID, and then add it to a collection. After the loop, I issue a single CREATE statement whereby the collection is persisted.

It used to be that this logic would have failed due to the flow element limit of 2000 counts. However, starting API version 57.0 this limit is eliminated. It appears that I am not hitting this limit any more, but now I am hitting another limit, i.e. Apex CPU limit, and as per my research, this is because I am exceeding the allocated 10-sec time for each flow interview.

Frankly, I find it unbelievable: 2000 campaign members is not exactly pushing a boundary. Surely there are campaigns with WAY MORE members than this! And, Salesforce wants folks to adapt to Flow?

There are some articles on the internet suggesting to use a batch apex. However, it is going to be a hard sell especially since Salesforce seems to have convinced everyone that flows are fantastic. Therefore my first preference is to look for ideas to fix this within a flow.

-- I tried dividing the logic into two flows: second flow is autolaunched, and it has a PAUSE step for 0 time, essentially a hack method to force a transaction commit. It's failing again with an error: "We couldn't pause this automation because it exceeded the maximum interview size of 1,000,000 characters."

-- I could make this logic into a record triggered action, and then consider executing this path asynchronously. However, I am not sure if a SELECT, followed by a loop, followed by a CREATE is suited to an asynchronous path and whether it will solve any issues or will hit some other wall? Consider, that if I made it to an asynchronous path, then they all will need to be together in the same flow logic, because a record-triggered flow does not give me any means to pass additional parameters (e.g. I cannot break the initial SELECT into a separate flow and then pass the resulting collection to the Record Triggered asynchronous path, etc.).

Any thoughts? Thank you in advance!

Upvotes: 1

Views: 987

Answers (1)

user25473323
user25473323

Reputation: 1

My simple question is would you able to process some number of member today and continue tomorrow?

I have a schedule trigger flow process that does the deduplication of the contacts. Inside the loop I have a get element to retrieve the get the original record based on some criteria. After entering the loop I have a count that increases by one. Before going to the next record, I have a decision element and check if the count is = to 150 (that is the number I have set for my process).

Upvotes: 0

Related Questions