Wednesday 24 May 2017

Steps to Hide the Print Button for a User having a Custom Customer Center Role when a Sales Order is Viewed on the Web Store Context

​For a custom Customer Center role having Fulfill Sales Order=None, when a user clicks See Orders in theCustomer Center > view any order > a Print button is displayed. To hide this Print button, here are the steps:

1. Create a user event script by navigating to Customization > Scripting > Scripts > New > User EventUser Event needs to be setup this way: 

Deployments 
Applies to: Sales Order 
Deployed: Yes 
Status: Released 

Scripts
Before Record Load: restrictPrintButtonOnWebStore <-- this is the name of the function 

Script :

function restrictPrintButtonOnWebStore(type, form){ 
        var currentContext = nlapiGetContext(); 
        if (currentContext.getExecutionContext() == 'webstore') { 
                nlapiLogExecution('Debug', 'context', currentContext.getExecutionContext()); 
                userRole = nlapiGetRole(); 
                nlapiLogExecution('Debug', 'nlapiGetRole()', userRole); 
                if(userRole == 1017){ 
                      var button = form.getButton('print'); 
                      button.setVisible(false); 
                } 
                 
        } 
}

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: