Hello, I am working on a SuiteLet which merges 2 s...
# suitescript
k
Hello, I am working on a SuiteLet which merges 2 saved searches (primary key being the Customer's Internal ID) and displaying the merged result as a List in the Suitelet. I am using the following function to merge the 2 searches. Everything seems to work fine with few data, but is timing out when I am running it for all the results of the search i.e 404 and 489 for the 2 searches. Is there any way I can make the following function more efficient so the script does not time out?
Copy code
function mergeSearches(ARCustomer, AveragePayDay) {
    var return_back = [];
    ARCustomer.run().each(function (result) {
        var ARCustomer_id = result.getValue({
            name: "internalid",
            join: "customerMain",
            summary: "GROUP"
        });

        var averagepayday;
        AveragePayDay.run().each(function (result2) {
            var AveragePayDay_id = result2.getValue({
                name: "internalid",
                join: "customerMain",
                summary: "GROUP"
            });
            if (ARCustomer_id == AveragePayDay_id) {

                averagepayday = result2.getValue({
                    name: "formulanumeric",
                    summary: "AVG"
                });
                return false;
            }
            return true;
        });

        return_back.push(resultToObject(result, averagepayday));
        return true;
    });
    return return_back;
}
z
Is this merging in the suitelet or client side on it?
You may need to paginate it
k
Its in the suitelet itself
z
Can you log the usage? It may be hanging
Or you are running an each in an each so you are basically doing just under 200,000 eaches?
k
yes it should be a lesser than 16000
The governance usage before looping is 1000 by the time the error pops up it drops to -10
s
consider rewriting the thing as a single SuiteQL search if possible?
a
Find a common point between the two searches, do one search and build an object taking that into consideration, do the second search and build and object considering the common point merge your resulting objects or array of objects using interpolation. Avoid doing two arrays of results and then ending up with nested loops, your main problem right now.
👍 1
b
id like to emphasize what alien4u said. its not viable to run the second search for every singe search result of another search. run each search once then merge them.
m
2021.2 has new feature called dataset linking which might work for what you are trying to do
k
thank you all!