Hi, I'm coding a search function to look up subsid...
# general
r
Hi, I'm coding a search function to look up subsidiaries in a Netsuite account. Actually, I'm using the WebStorm plug-in for Netsuite to compile my TypeScript code into SuiteScript 2.1, thus deploying the latter to the Netsuite account. The code of the function is below:
Copy code
/**
 * Takes the subsource parameter and looks for the subsidiary ID corresponding to that soubsource. The field 'custrecord_lw_subsource' is defined to match the subsource parameter to the subsidiaries whose custrecord_lw_subsource == subsource.
 * @param subsource
 */
function searchSubsidiaryIdBySubSource(subsource) {
    try {
        // Creating the search using the N/search module
        let mySearch = search.create({
            type: search.Type.SUBSIDIARY,
            columns: ['internalid', 'name', 'custrecord_lw_subsource'],
            filters: ['custrecord_lw_subsource', 'contains', subsource]
        });

        // Running the search and getting the results.
        let myResultSet = mySearch.run();

        // Up to 100 results are retrieved.
        let resultRange = myResultSet.getRange({
            start: 0,
            end: 100
        });

        if (resultRange.length > 0) {
            log.debug({
                title: "Subsidiary found for " + subsource,
                details: resultRange.length + " subsidiary(ies) found for " + subsource + ". The subsidiary with the ID " + resultRange[0].id + " was selected."
            });
            return resultRange[0].id;
        } else return undefined;
    }
    catch (e) {
        log.debug({
            title: "Search error: " + e.name,
            details: "An error occurred while searching the subsidiary: '" + subsource + "': " + e.message
        });
        // If an error occurs the subsidiary with the id 5 is given by default.
        return "5";
    }
}
Although I placed the code inside a try/catch clause, sometimes it's throwing an SSS_INVALID_SRCH_OPERATOR Error. It's strange because the error appears only for some values of the subsource. Moreover, I would expect the system to launch the errors through the catch sentence but it's thrown by another way. Can you suggest something about this strange behavior? I'm afraid of loosing data when I deploy the script to production...
m
You need to validate that
subsource
is set before running the search. Something like this before the try/catch would work. (Depends on
subsource
type)
Copy code
if (isNaN(parseInt(subsource))) return;
b
you probably dont want to implement your check like the above suggestion if
subsource
is text
just check that it isnt an empty value using
Copy code
if (!subsource) {
  // throw error or return early here
}
r
I've done that, and the subsource is set (it's a string) but the error persists. I continue debugging, thus placing messages for each line to detect where is the problem. Thanks...
m
Try replacing
'contains'
with
search.Operator.CONTAINS
. Just in case the enum value is different. Are you returning or throwing an error to make sure it doesn't go any further?
Try this. Note it is written assuming your using version 2.1. Otherwise you will need to alter slightly for your version.
Copy code
/**
 * Takes the subsource parameter and looks for the subsidiary ID corresponding to that soubsource. The field 'custrecord_lw_subsource' is defined to match the subsource parameter to the subsidiaries whose custrecord_lw_subsource == subsource.
 * @param subsource
 */
function searchSubsidiaryIdBySubSource(subsource) {

    let subsidiary;

    if (!subsource) return "5";

    // Creating the search using the N/search module
    search.create({
        type: search.Type.SUBSIDIARY,
        columns: ['internalid', 'name', 'custrecord_lw_subsource'],
        filters: [{ name: 'custrecord_lw_subsource', operator: search.Operator.CONTAINS, values: subsource }]
    })
        .run()
        .each(subsidiary_result => {
            subsidiary = subsidiary_result.id;
        });
    
    return subsidiary;
}
Note: I think your filters should be and array in an array.
Copy code
[['custrecord_lw_subsource', 'contains', subsource]]