Sunday 7 May 2017

Netsuite Suitescript 2.0

Its been a while since suitescript 2.0 made an appearance into Netsuite with Version 2015 Release 2. Netsuite introduced a SuiteScript 2.0- a complete re-factor of the SuiteScript model driving towards a more modular based development approach. Recently, I was trying to have some hands on it and thought of sharing few keynotes based on my exploration.
Advantages to SuiteScript 2.0
  1. Modular Architecture
  2. Modern JavaScript Syntax and Behavior
  3. New and Improved API Functionality
  4. Asynchronous Client Side Processing (Promises)
  5. New Batch Processing Framework (Map/Reduce Script Type)
Lets see some examples:
Load a record


require(['N/record'], function(RECORDMODULE){

    var recordType = RECORDMODULE.Type.ACCOUNT; //The type of record to load.
    var recordID = 10011; //The internal ID of the existing record in NetSuite.
    var isDynamic = true; //Determines whether to load the record in dynamic mode.

    var loadedRecord = RECORDMODULE.load({
        type: recordType, 
        id: recordID,
        isDynamic: isDynamic,
    });
});


Create Search

We can use search.create to build out our search object or search.load to load a saved search. Then we can  invoke run on the resulting search object. Finally, we will process the results in two ways:
  1. Use the each method and a callback
  2. Use the getRange method to get a specific number of results

In the example below, I've imported N/search into my module as s and shown the usage of the each method.
  • Summary Search (Suitescript 2.0)
// Assuming N/search is imported as `s`
var demoSalesOrderSearch = s.create({
    type: 'salesorder'
    // Use the summary property of a Column to perform grouping/summarizing
    columns: [{
        name: 'salesrep',
        summary: s.Summary.GROUP
    },{
        name: 'internalid',
        summary: s.Summary.COUNT
    }],
    filters: [{
        name: 'mainline',
        operator: 'is',
        values: ['T']
    }]
});

demoSalesOrderSearch.run().each(function (result) {
    var repId = result.getValue({
        "name": "salesrep",
        "summary": s.Summary.GROUP
    });
    var repName = result.getText({
        "name": "salesrep",
        "summary": s.Summary.GROUP
    });
    var orderCount = parseInt(result.getValue({
            "name": "internalid",
            "summary": s.Summary.COUNT
    }), 10);

    log.debug({
        "title": "Order Count by Sales Rep",
        "details": repName + " has sold " + orderCount + " orders."
    });
});
  • Search with criteria search
require(['N/search'], function(SEARCHMODULE){
    
    var type = 'transaction';
    var columns = [];
    columns.push(SEARCHMODULE.createColumn({
        name: 'internalid'
    }));
    columns.push(SEARCHMODULE.createColumn({
        name: 'formulanumeric',
        formula: '{quantity}-{quantityshiprecv}'
    }));
    
    var salesOrdersArray = [1001,2001,3001,4001];
    var filters = [];
    filters.push(['type', 'anyof', 'SalesOrd']);
    filters.push('and');
    filters.push(['mainline', 'is', 'F']);
    filters.push('and');
    filters.push(['internalid', 'anyof', salesOrdersArray]);
        
    var mySearchObj = {};
    mySearchObj.type = type;
    mySearchObj.columns = columns;
    mySearchObj.filters = filters;
    
    var mySearch = SEARCHMODULE.create(mySearchObj);
    var resultset = mySearch.run();
    var results = resultset.getRange(0, 1000);
    for(var i in results){
        var result = results[i];
        var row = {};
        for(var k in result.columns){
            log.debug('Result is ' + result.getValue(result.columns[k])); //result 
        }
    }
});
  • From Saved search
require(['N/search'], function(SEARCHMODULE){
    var savedSearchId = 'customsearch_customsearch';
    var mySearch = SEARCHMODULE.load(savedSearchId);
    var resultset = mySearch.run();
    var results = resultset.getRange(0, 1000);
    for(var i in results){
        var result = results[i];
        for(var k in result.columns){
            log.debug('Result is ' + result.getValue(result.columns[k])); //result
        }
    }
});

If you want to explore more related to Suitescript 2.0 check out the below resources:



6 comments:

  1. The result.getValue(result.columns[k]) sometimes gives me a number instead of the text value that I see when the saved search has run. I am not sure why. Is there something else I need to be doing to get the text value that I see in the saved search results?

    ReplyDelete
    Replies
    1. It seems to be returning an id to some other field. Fields with the same number will have the same text value in the saved search output.

      Delete
    2. Nobody ever came back with an answer, but if you stumble across this here is the answer.

      if(result.getText(result.columns[k]) == null){
      log.debug('Result is ' + result.getValue(result.columns[k])); // Result
      } else {
      log.debug('Result is ' + result.getText(result.columns[k])); // Result
      }

      Delete
  2. Thanks for the information. the information you provided is very helpful for NetSuite Training learners. Nice blog

    ReplyDelete
  3. Nice post content is impressive to read ,Thanks for proving some helpful information.Hope you will keep on sharing articles.
    This provides good insight. You might also be interested to know more about generating more leads and getting the right intelligence to engage prospects. Techno Data Group implements new lead gen ideas and strategies for generating more leads and targeting the right leads and accounts.
    TECHNO DATA GROUP

    ReplyDelete
  4. if you're looking for a NetSuite expert in San Francisco, look no further! i've over 10 years of revel in with NetSuite and can help you with the entirety from implementation to customization to training. i am additionally a licensed NetSuite professional, so you may be positive that i have the knowledge and skills to help you get the maximum out of NetSuite.
    San Francisco NetSuite Expert

    ReplyDelete