Search.create is Breaking my For Loop
I’m running a search inside a for loop. The loop is iterating through an array and creating custom records for each index value. But, I want to make sure there isn’t a custom record already for that index, so I’m running a search with each iteration to check for that.
The bizarre thing is the search creation is causing my loop to go haywire. After the code hits search.create, my “i” variable increments all the to the last index value, completing only one iteration before exiting the loop.
I’ve isolated the search as the cause. When I comment it out, the loop works just fine. Anyone ever experience this? I have no idea why this would happen.
Here is a screenshot of the search. I’ve already tried using static filter values instead of variables (in case anyone suspects that’s the problem) and I got the same results.
Have you tried creating the search record once out of the loop and just updating the filters or filterExpression on each iteration?
Also, If you’re trying to maintain the uniqueness of a custom record being a combination of the event, attendee, and training couldn’t a user event before Submit work as well ? If create context you find a match in the beforeSubmit you could just throw an error and in the loop creating the custom record just deal with that error.