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

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.

Rookie Answered on September 19, 2019.

The problem is if I wait till I’m out of the loop to search, the custom record will already be created and it might create a duplicate. The search is supposed to be an initial check in the iteration before creating the custom record.

In terms of the user event script, the problem is I’m triggering the creation of these custom records solely through a client script, so the user event would not be triggered, I guess unless someone accesses the record after the fact. At that point, the best you could do it notify the user it’s a duplicate when they open the record. But I’d like to prevent the duplicate instead of simply detecting it. So I don’t think that will work, unless I’m misunderstanding what you’re getting at.


on September 19, 2019.
Add Comment

Your Answer

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