RE: 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.

RE: Search.create is Breaking my For Loop

mmascitto Beginner Asked on September 19, 2019 in SuiteScript.

Is it possible “i” is global (not declared with var)?

on September 19, 2019.

Well, that’s embarrassing. Yes, you’re right!

on September 19, 2019.

As @rtanner mentioned, the main issue seems to be with variable scopping and also I noted a few issues with your code.

From performance perspective, you should not create searches in loop as it would consume a lot of time and execution points and as you mentioned, you are using the above script in client script, so you should make a few changes.

  1. Use search.create.promise as it won’t cause UI to lag.
  2. Use single search using ORĀ in filterExpressions.

Once you have the searchResults filter out the records which are not present and create them.


on September 20, 2019.

The problem is each search is unique per array index. So I don’t see any way around it. As I iterate through the array, I have to run a search with the unique index parameters. I can’t search just one time and be done with it, unless I’m missing something.

I guess maybe you’re saying to use OR to combine each index set of values/scenario into one search … but then I’ll need to figure out a way to map the results back to the array indexes so I know which indexes returned results and which didn’t, because the search is there to prevent duplicates and tells me whether or not to create a custom record for each array index.

Right now, the logic is easy. It grabs the index values, searches for an existing custom record, and if the search returns zero, then it creates a custom record for that iteration. I’m not crazy about running a search for every loop iteration, this just seemed the most straightforward way to do it.

on September 20, 2019.

You can simply add a searchColumn for event, attendee or training (whichever is unique) or all of them. Its just that current approach has a few limitations but you are right your approach is easy.

on September 20, 2019.
Add Comment
2 Answers

It seems like you are using search API inside the loop and consumes a lot of governance that would affect the user interaction which is not a good practice.

Beginner Answered on September 20, 2019.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.