• : 1-877-596-8218
  • : sales@aecc.ca
# # #
#

How to create a NetSuite Custom Plugin using SuiteScript 2.0.

NetSuite provides a way the user can add their own plugin into system. Other scripts can instant the plugin and call the function in the plugin to avoid repeat codes.

Let’s start from building a plugin and install it in the system
We will build a plugin that find all the Sales Orders in the system and return the internal ID of them as an array:


/**
**  Plugin Entry Function
**
**/
function findAllSalesOrders() {
    //define saved search filters and columns 
    var columns =[];
    var filters =[];
    columns[0] = new nlobjSearchColumn('internalid');
    columns[1] = new nlobjSearchColumn('recordtype');
    //call saved search
    var results = savedSearch('salesorder', null, columns);
    var salesOrdersID = [];
    if(results){
        nlapiLogExecution('debug', 'length is ', results.length);
        results.forEach(function(result){
            salesOrdersID.push(result.getValue('internalid'));
        });
    }
    return salesOrdersID;
}

/**
**  Saved Search Function
**
**/
function savedSearch(recordType, searchFilter, searchColumns) {
    var arrSearchResults = [];
    var count = 1000, min = 0, max = 1000;
    var searchObj = false;
    var columns = [];
    if (isEmpty(searchColumns)) {
        searchObj = nlapiCreateSearch(recordType, searchFilter, columns);
    } else {
        searchObj = nlapiCreateSearch(recordType, searchFilter, searchColumns);
    }

    var rs = searchObj.runSearch();

    while (count == 1000) {
        var resultSet = rs.getResults(min, max);
        arrSearchResults = arrSearchResults.concat(resultSet);
        min = max;
        max += 1000;
        count = resultSet.length;
    }

    if (arrSearchResults) {
        nlapiLogExecution('DEBUG', 'searchAllRecord', 'Total search results(' + recordType + '): ' + arrSearchResults.length);
    }

    return arrSearchResults;
}

function isEmpty(stValue) {
    if ((stValue == null) || (stValue == '') || (stValue == ' ') || (stValue == undefined)) {
        return true;
    } else {
        return false;
    }
}

Then we go to install this script