I am looping through a saved search and getting so...
# suitescript
m
I am looping through a saved search and getting some values from a custom record and setting it on the customer record. I was testing it with a single internal id (customer) and it was doing what I needed. Now I have removed the internal id it's not quite working. Customer A - "a", "b", "c". Customer B - "d". I need a way to process each customer, right now it's getting "a", "b", "c", "d" for both customers. How do I add a filter on this or what do I need to do?
Copy code
ssRowCount.pageRanges.forEach(function (pageRange) {
                    const data_page = ssRowCount.fetch({
                        index: pageRange.index
                    });
                    data_page.data.forEach(function (result) {
                            oldProfileCollection = result.getValue({
                                name: 'custentity_customer_profile',
                                summary: 'GROUP'
                            });
                            ssInternalId = result.getValue({
                                name: 'internalid',
                                summary: 'GROUP'
                            });
                            ssCusProfile = result.getText({
                                name: 'custrecord_profile',
                                join: 'CUSTRECORD_LOCATION',
                                summary: 'GROUP'
                            });

                            newProfileCollection.push(ssCusProfile);

                        }
                    );
                });
setProfile(record, oldProfileCollection, ssInternalId, newProfileCollection);
s
It’s hard for me to tell from the field names alone, but is one of the fields on the custom record a List/Record Customer field? if so, then pull that field into the search, GROUP on it, and use that as the internal id for setting the values on the customer dynamically. If you don’t have the customer as a field on the record type, then the next question is, do you have a field (or set of multiple fields), whose values exactly match fields on the customer record? If so, you could likely transform your saved search into a SuiteQL query, joining the Customer to the custom record type by those field values, and then you’d have the customer internal id to use.
m
It's a customer saved search so I have the customer internal ID. I also have the values from a custom record. However, for each customer, there might be multiple lines with the same data and as I loop through these line I want to basically process one customer and skip to the next customer. I think I need a filter in there...
Here's an example of 2 customers. 1 has a single line, the other has 3 lines shown in the old profile. The new profile is after I ran the script:
s
If you are only GROUPing on the customer id and the values that need to be set, then multiple instances should collapse into one result.
So, did the customer have three lines before the script ran? Or only after the script ran?
what were the actual search results, and what did you want the outcome to be? one new profile with only the last three values?
m
Yes, the search already had 4 lines. Three for 1 customer and 1 for the other customer. If I group by the customer internal id - I wouldn't be able to access the other fields data. The outcome - Customer 1 - FFBB. Customer 2 - SJBB, SJPR, TUBB
s
Okay, then I have a better suggestion: Use a Map/Reduce script as it is perfect for this kind of situation, collapsing multiple results into one. In this example, in the map phase of an M/R script, you write to the reduce context the following key-value pairs: key: Customer 1 id, value: FFBB key: Customer 2 id, value: SJBB key: Customer 2 id, value: SJPR key: Customer 2 id, value: TUBB Then, in the reduce phase, you’d only have two instances run, each with the following context: key: Customer 1 id, values: [FFBB] key: Customer 2 id, values: [SJBB, SJPR, TUBB] You could then just create the new profile by joining or concatenating the values together, but they’d already be combined together into a single array for the shared key by the Map/Reduce engine.
you also don’t need to worry about paging through the search results, just return the search itself from the getInputData phase, and NetSuite will run it for you, and serialize the results as JSON
m
Hmm, kinda was hoping to just use the script that make some minor changes. Even if I have to write to the field 3 times, for now I just need some demo setup. Could consider optimised solution later.
s
Well, another solution is, on the first pass through your search results, write the key-value pairs to a JS Map. Check if the key (customer id) already exists in the map, and if not, just write it and the value as an array of one element. If the key does exist, just push the new value into the array by key. Then, when done looping through the search results, loop through all of the Map’s keys, and you’ll have an array of values for each customer, the same as if you’d done it via a Map/Reduce
👍 2
An, lastly, you could always load/check/update the customer on each pass through the search results. least invasive code-wise, but performance will be far worse, and it will use much more governance as well.
👍 1
m
OK, thanks for the suggestions.