Search code examples
netsuitesuitescript

Define syntax for Suitescript 2.1: Fail to evaluate script after adding 'N/search' to define


The following script is deployed and runs correctly:

/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define([],

function() {
    const lineFieldLookup = [
      'custcol_sas_adapter', 'custcol_sas_body', 'custcol_sas_box', 'custcol_sas_endcap',
      'custcol_sas_lidcap', 'custcol_sas_mortrclr', 'custcol_sas_snout', 'custcol_sas_strmclr',
      'custcol_sas_topclamp', 'custcol_sas_tplate'];

      function validateLine(scriptContext) {
           const fieldValuesTable = lineFieldLookup.map((fieldId) => {
             return ({
               fieldId: fieldId,
               value: scriptContext.currentRecord.getCurrentSublistValue({sublistId:'item', fieldId: fieldId })
             })
           });
        console.log(fieldValuesTable)
    }

    return {
       validateLine: validateLine,
    };
});

However, adding 'N/search' dependency to the script via browser 'edit' and clicking save throws an error.

Updated Code:

define(['N/search'],

function(search) {
  // nothing changed here
}

The error:

Fail to evaluate script: {"type":"error.SuiteScriptModuleLoaderError","name":"UNEXPECTED_ERROR","message":"","stack":[]}

What's preventing me from being able to add the search dependency?


Solution

  • I think it's time to learn the N/query ;)

    As client side script you may load the N/search using require.

    /**
     * @NApiVersion 2.1
     * @NScriptType ClientScript
     * @NModuleScope SameAccount
     */
    define([],
        function () {
            function loadSearch(callback) { require(['N/search'], search => { callback(null, search); }); }
            function pageInit(context) {
                try {
                    loadSearch((error, search) => {
                        if (!error) {
                            let c_data = search.lookupFields({
                                type: search.Type.CUSTOMER,
                                id: "4390463",
                                columns: ["altname", "email", "address"]
                            });
                            console.log('c_data', c_data);
                        }
                    });
                } catch (e) {
                    console.error('loadSearch', e);
                }
            }
            return {
                pageInit: pageInit
            };
        });