Netsuite Suitelet

What is NetSuite SuiteLet?

A NetSuite SuiteLet is an extension of NetSuite SuiteScript that facilitates the building of customized pages as well as back-end logics. It is a server-side script and is apt for a request-response model operating. Further, it can be easily triggered by either HTTP GET or else POST requests. A GET request from a web browser can also be used to invoke it. 

So, let’s try to create a simple SuiteLet to trigger and insert an “Add-on” button on a customer record. Whenever the record is being clicked, the NetSuite will open and display the information associated with that specific customer. 

First, we should create a user event before loading the script to add a button to a customer record. Once this button is clicked, the user event script opens the NetSuite and passes the essential parameters into it from the URL. 

Let’s try to understand how a custom button is created to display a page along with custom HTML code.

Example:

function beforeLoad_showAllCustomers(type, form, request) {

var CONTEXT = nlapiGetContext();

if (nlapiGetRecordType() == ‘customer’) {

if (type == ‘view’) {

form.setScript(CONTEXT.getScriptId());

form.addButton(‘custpage_showallcustomers’, ‘Show all customers’, ‘executeSuiteletForm()’);

}

}

}

function executeSuiteletForm() {

var params = ‘/app/site/hosting/scriptlet.nl?script=397&deploy=1’;

var stWidth = ‘1200’;

var stHeight = ‘800’;

window.open(params, “All bins”, ‘left=500px, top=100px, resizable=no, scrollbars=no, height=’ + stHeight + ‘, width=’ + stWidth);

}

In the above example, the first function modifies the form before it gets loaded. Technically, it adds an additional button with the name “custpage_showallcustomers” on the page along with the caption “Show all customers” and event “executeSuiteForm()”.  This function will be called repeatedly whenever the user presses the button and opens a new form along with custom HTML code. 

To create NetSuite SuiteLet Script in Netsuite follow these below steps:

  1. Save the code as the JavaScript file
  2. Go to the “Customization” tab and click on it
  3. You will get a drop-down menu, in that, select the “Scripting” option
  4. Again, you will get a pull-down list, in that, select the “Script” option
  5. Then, click on the “New” option in the next drop-down menu

In simple words: Customization -> Scripting -> Scripts -> New

  1. Upload the JavaScript file
  2. In case, your script file is in 1.0, then it will open into another screen and offer many options. In that select your preferred SuiteLet. If your script file is in 2.0, it will open onto the script page and display default data extracted from the script file. The function named “autopopulate” will be disabled on this page. 
  3. Click on the “Save” button to store all the updates and deploy the script
  4. On the Deployment page, enter the details such as Deployment ID, status, etc.
  5. The checkbox, titled “Available Without Login” will define whether this SuiteLet is internal or external. 
  6. In case, this SuiteLet is external, then three URLs will be displayed on  the screen
  7. Again, click the button “Save” option for the deployment

Now, let’s try to create a NetSuite SuiteLet to implement this example’s business logic.

Example

function suitelet_showAllCustomers(request , response) {

var context = nlapiGetContext ();

var method = request.getMethod().toUpperCase();

if (method == ‘GET’) {

var stHtml = “”;

stHtml += “”;

stHtml += generateForm();

stHtml += “”;

response.write (stHtml);

}

}

function generateForm() {

var data = getData();

var result = ”;

for (var i = 0; i < data.length; i++) {

var obj = data[i];

result += ” + obj.internalId + ” + obj.companyname + ”;

}

return ” +

” +

” +

” + result +

Internal ID__________company name

‘;

}

function getData() {

function modifyFilters(assocArray) {

var result = [];

for ( var key in assocArray) {

result.push(assocArray[key]);

}

return result;

}

var filters = [];

var columns = {};

columns[“internalid”] = new nlobjSearchColumn(‘internalid’);

columns[“companyname”] = new nlobjSearchColumn(‘companyname’);

filters.push(new nlobjSearchFilter(‘stage’, null, ‘is’, ‘CUSTOMER’));

var data = (nlapiSearchRecord(“customer”, null, filters, modifyFilters(columns)) || []).map(function(rec) {

var internalId = rec.getValue(columns[“internalid”]);

var companyname = rec.getValue(columns[“companyname”]);

return {

internalId: internalId,

companyname: companyname

}

});

return data;

}

Here, “suitelet_showAllCustomers” is an entry-point function. If the request of GET type, then you will be able to extract the customers’ list from the system with the help of nlapiSearchRecord. 

What is a NetSuite SuiteLet Script?

You will be able to create a custom record as well as use a user script with much ease. However, importing data from the saved search on a screen and customizing data during its display on the screen can’t be possible. Creating a form via functionality will not be supported here as well. In such cases, you must make use of SuiteLet Scripts.

 

Well-known as the core components of any NetSuite app, NetSuite SuiteLet scripts are used to display a form and process the data. Besides, it can be used to import or post the data as well. Usually, most of the firms create pages by using SuiteLets and attempt to set multiple preferences.

 

There are two types of SuiteLets available at the present. They are, Internal SuiteLet and External SuiteLet.

Internal SuiteLets:

As the name suggests, these are utilized inside the NetSuite, only after when you log in and click on the shortcut/URL for that SuiteLet. These are mostly used by internal users for many purposes. For instance:

  • You desire to limit the access of a custom record to specific users only based on their roles
  • You want to create a record using SuiteLet and submit it for the processing
  • You wish to build your own Approval screen specially meant for SO’s and PO’s. Also, you may permit anyone for using the screen and approving multiple SO’s and PO’s

Note: The function onRequest(params) is an entry-point and can be added before defining the function. In such a case, if you use the NetSuite API then the coding will appear as below:

In Suite Script 1.0

function yourFirstSuiteletInternal(request, response) {   

if (request.getMethod() == ‘GET’ )

{

var form = nlapiCreateForm(‘Simple Form’);    

var field = form.addField(‘custpage_text’,’text’, ‘Text’);     

form.addSubmitButton(‘Submit’);   

response.writePage( form );  

}   

else 

{

response.write(‘write your logic here’ );  

}

}

In Suite Script 2.0

/**  

*@NApiVersion 2.x  

*@NScriptType Suitelet  

*/ 

// this creates a Suitelet form, add a field, and get the value on submission. 

define([‘N/ui/serverWidget’],

function(ui) {    

function netsuiteGuruSuitelet(context) { 

if (context.request.method === ‘GET’) {            

var form = ui.createForm({                

title: Netsuite Guru Form’     

});

var subject = form.addField({                

id: ‘custpage_text’,                

type: ui.FieldType.TEXT,                

label: ‘Text’            

});

                     form.addSubmitButton({                

label: ‘Submit’            

});

context.response.writePage(form);        

} else {

var request = context.request;            

var mText = request.parameters.custpage_text 

log.debug({   

title: ‘mText ‘,     

details: ‘mText’ +mText     

});        

}    

}    

return { 

         onRequest: netsuiteGuruSuitelet    

};

});     

External SuiteLets:

As the name implies, these SuiteLets are used to show something that lies outside of the NetSuite. For this, no login is required and any user can access it. Most of the cases, the URLs of such kinds of SuiteLets are sent through emails or else provided on the official websites. These URLs will be quite difficult to remember, lengthy, and case sensitive. However, if you are making use of GET function as well as parameters, then keep the logic as it is so that it should not break the limitation of the URL for the GET. 

You can use a NetSuite API for creating an External SuiteLet, but, most of the time it might not be compatible with your website. At such times, you can easily customize through JavaScript, HTML, and CSS. The best examples of these kinds of SuiteLets are RMA, Registration Form, or Contact Us. 

The NetSuite SuiteLet HTML coding for an External SuiteLet looks like this:

In Suite Script 1.0

function yourFirstExternalSuitelet(request, response) 

{

if (request.getMethod() == ‘GET’ )

{

var html = ‘<!DOCTYPE html>’;

html += ‘<html’>;

//you can add your head section, CSS  and javascripts

html += ‘<body>’;

html += ‘<form>’;

  html += ‘  First name:<br>’;

html += ‘ input type=”text” name=”firstname”>’;

html += ‘ <br>’;

html += ‘  Last name:<br>’;

html += ‘ input type=”text” name=”lastname”>’;

html += ‘<form>’;

html += ‘<p>Note that the form itself is not visible.</p>’;

html += ‘<p>Also note that the default width of a text input field is 20 characters./p>’;

html += ‘</body>’;

html += ‘</html>’;

      response.write( html );  

}   

Else

{

response.write(‘none found’ );   

In Suite Script 2.0: 

/** 

*@NApiVersion 2.x 

*@NScriptType Suitelet 

*/ 

//simple html page using external suitelet 2.0 

 

define(‘N/search’, function(search) {

function onRequest(params) {         

var html = ‘<!DOCTYPE html>’;

html += ‘<html>’;

//you can add the head section, CSS  and javascripts here.

html += ‘<body>’;

html += ‘<form>’;

html += ‘  First name:<br>’;

html += ‘ input type=”text” name=”firstname”>’;

html += ‘ <br>’;

html += ‘  Last name:<br>’;

html += ‘ input type=”text” name=”lastname”>’;

html += ‘<form>’;

html += ‘<p>Note that the form itself is not visible.</p>’;

html += ‘<p>Also note that the default width of a text input field is 20 characters./p>’;

html += ‘</body>’;

html += ‘</html>’;

params.response.write({ output: html });    

}   

return {  

onRequest: onRequest   

};

});

Call SuiteLet from Client Script 

A Client SuiteLet is capable of working for SuiteLets with NetSuite APIs. When you are making use of NetSuite SuiteLet HTML that time you must write all the coding for validation in JavaScript under the HTML’s head section. However, the usage of single quotes, as well as double quotes, might cause problems occasionally. So, whenever your code does not work properly, then make sure that you use F12 (source code). You might observe some kind of error on NetSuite SuiteLet HTML highlighted by the color red. 

A typical NetSuite Suitelet example: 

var CONTEXT = nlapiGetContext();

function beforeLoad_createBinButton(type, form, request) {

var stLoggerTitle = ‘beforeLoad_createBinButton’;

nlapiLogExecution(‘DEBUG’, stLoggerTitle, ‘||| — >>> Starting ‘ + stLoggerTitle + ‘ <<< — |||’);

form.setScript(CONTEXT.getScriptId());

form.addButton(‘custpage_displaybins’, ‘Bins’, ‘executeSuiteletForm()’);

nlapiLogExecution(‘DEBUG’, stLoggerTitle, ‘||| — >>> Exiting ‘ + stLoggerTitle + ‘ <<< — |||’);

}

function executeSuiteletForm() {

var customerInfo = nlapiLookupField(‘customer’,nlapiGetRecordId(),[‘internalid’, ‘entityid’]);

var customerID = customerInfo[‘internalid’];

var entityID = customerInfo[‘entityid’]

nlapiLogExecution(‘debug’, ‘customer ID is ‘, customerID);

var URL = nlapiResolveURL(‘Suitelet’, ‘customscript_create_bin_list’, ‘customdeploy_create_bin_list’);

var URLParam = ‘&customerID=’ + customerID + ‘&entityID=’ + entityID;

var suiteLetURL = URL+ URLParam;

window.open(suiteLetURL);

}

In the above example, both customer ID as well as entity ID are moved into the SuiteLet with the request URL. 

Now, let’s have a look at the script writable for creating a SuiteLet for searching and listing all the information linked with this customer.

/**

* Created by author-name on 2020-2-28.

*/

/**

* SuiteLet to display the list of bins for customer

*/

function suitelet_displayCustomerBin(request, response){

nlapiLogExecution(‘debug’, ‘—– Display Bins SuiteLet runs —–‘);

if(request.getMethod() == ‘GET’){

var customerID = request.getParameter(‘customerID’);

        var customerName = nlapiLookupField(‘customer’,customerID,’altname’);

        var entityID = request.getParameter(‘entityID’);

       

//—– Create Form and set body field

var form = nlapiCreateForm(‘Bins List’);

form.addField(‘custpage_customer_name’, ‘text’, ‘Customer Name’).setDefaultValue(customerName);

//.setDisplayType(‘inline’);

form.getField(‘custpage_customer_name’).setDisplayType(‘inline’);

form.addField(‘custpage_customer_id’, ‘text’, ‘Customer Internal  ID’).setDefaultValue(customerID);

form.getField(‘custpage_customer_id’).setDisplayType(‘hidden’);

form.addField(‘custpage_customer_entityid’, ‘text’, ‘Customer Entity ID’).setDefaultValue(entityID);

form.getField(‘custpage_customer_entityid’).setDisplayType(‘hidden’);

 

//—-search bins and write to sublist—–

var binSublist = form.addSubList(‘custpage_binsublist’, ‘list’, ‘Bins’);

binSublist.addField(‘custpage_location’, ‘select’, ‘Location’, ‘location’).setDisplayType(‘inline’);

binSublist.addField(‘custpage_aptos_actual_bin’, ‘select’, ‘Aptos Actual Bin Name’, ‘customlist_aptos_bins’).setDisplayType(‘inline’);

binSublist.addField(‘ccustpage_bin’, ‘select’, ‘Bin Number’, ‘bin’).setDisplayType(‘inline’);

for (var i = 1; i <= bins.length; i++) {

           var bin = bins[i – 1];

           binSublist.setLineItemValue(‘custpage_location’, i, bin.locationid);

           binSublist.setLineItemValue(‘custpage_aptos_actual_bin’, i, bin.actualbinname);

           binSublist.setLineItemValue(‘ccustpage_bin’, i, bin.internalid);

}

form.addSubmitButton(‘Submit’);

response.writePage(form);

      

}else{//POST

// —– Redirect to SuiteLet ——

var params = new Array();

params[‘customerID’] = customerID;

params[‘entityID’] = entityID;

nlapiSetRedirectURL (‘SUITELET’ , ‘customscript_create_bin_list’ , ‘customdeploy_create_bin_list’,false, params);

}

}

 

If you want some additional information for SUITELET then connect our Netsuite Experts and call +61-480-016-233

Posts created 17

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top