2

I am having trouble understanding what this error means.

org.mozilla.javascript.EcmaError: ReferenceError: "nlapiSearchRecord" is not defined. (/SuiteScripts/PreventDuplicateCustomer.js#35)

I am attempting to create a script which searches for a duplicate record based on a specific field which is supposed to be unique. I would like to prevent creating duplicate 'CUSTOMER' records in NetSuite using the following script. '

Does anyone see anything that jumps of the page as wrong with the code below?

    // 2.0
define(["N/error", "N/log"], function (err, log) {

    /**
     * User Event 2.0 example showing usage of the Submit events
     *
     * @NApiVersion 2.x
     * @NModuleScope SameAccount
     * @NScriptType UserEventScript
     * @appliedtorecord customer
     */
    var exports = {};

    function beforeSubmit(scriptContext) {      
        log.debug({
            "title": "Before Submit",
            "details": "action=" + scriptContext.type
        });

        if (doesCustomerExist(scriptContext)) {
            throw err.create({
                "name": "DUPLICATE_SFDC_ACCOUNT_ID",
                "message": "Customer Already Contains SFDC Account Id",
                "notifyOff": true
            });
        }
    }

    function doesCustomerExist(scriptContext) {
        var sfdcAccountId = scriptContext.newRecord.getValue('custentitysfdc_account_id');
        if(sfdcAccountId == null || sfdcAccountId == '') return false;

        var searchFilter = new nlobjSearchFilter('custentitysfdc_account_id', null, 'is', sfdcAccountId, null);
        var searchResult = nlapiSearchRecord('customer', null, searchFilter, null);

        return (searchResult != null && searchResult.length > 0);
    }

    exports.beforeSubmit = beforeSubmit;
    return exports;
});
Coova
  • 1,818
  • 5
  • 36
  • 63

1 Answers1

8

nlapiSearchRecord() is a SuiteScript 1.0 function and you're trying to call it from a SuiteScript 2.0 script.

You need to add the N/search module to your 2.0 script and use the functionality provided in that module to perform a search. In the NetSuite help documentation, navigate to SuiteCloud -> SuiteScript 2.0 -> SuiteScript 2.0 API -> SuiteScript 2.0 Modules -> N/Search Module to learn how to perform a search with SuiteScript 2.0.

Mike Robbins
  • 3,184
  • 15
  • 20