Netsuite Web Service alternative - Suitelet Tutorial

Netsuite soap-based web service interface (also known as SuiteTalk) is great for processing batch jobs, but may not be suitable for real-time form processing due to concurrency problem. For real-time processing, the Suitelets may be preferred over SuiteTalk as they don't have concurrent session problems.

According to Netsuite, the Suitelet is defined as "extensions of the SuiteScript API that give developers the ability to build custom NetSuite pages and backend logic". Suitelets are server-side scripts that operate in a request-response model. They are invoked by HTTP GET or POST requests to system generated URLs. For the purpose of this illustration, we'll only discuss using Suitelet as an alternative to SuiteTalk for implementing server-side "backend" logic although Suitelets also allow developers to create custom Netsuite client-side pages. . Instead of using PHPToolkit provided by Netsuite, we'll use cURL to make a HTTP GET or POST requests to Netsuite generated "external" URL where Suitelet is deployed.

To illustrate how Suitelet can be used to run server-side logic, we'll create a suitelet that creates a Customer Record in the Netsuite system much like how SuiteTalk web service creates a customer.

1. To use SuiteScript in your NetSuite environment, you'll have to enable it first. You'll have to login to Netsuite Portal as an administrator, and go to Setup -> Company -> Setup Tasks -> Enable Features. Then, you'll check mark Server SuiteScript checkbox under the SuiteFlex tab -> SuiteScript heading.

2. You'll then create a javascript containing Suitelet (Netsuite API used to manipulate Netsuite Records) code and upload it to File Cabinet. You may use any text editor to create a javascript file, but Netsuite recommends Eclipse as the preferred IDE. To upload a Suitelet, go to Documents -> Files -> SuiteScripts. Then, click on the "Add File" button located in the lower-left corner of the File Cabinet screen. If you need to update an existing Suitelet, you may upload the file again and Click OK to overwirte the existing file.

function CreateCustomerRecord(request, response)   {
	
    if (request.getMethod() == 'POST') {
    	var customer = nlapiCreateRecord('customer');
    	customer.setFieldValue('isperson', 'T');
    	customer.setFieldValue('firstname', request.getParameter('firstname'));
    	customer.setFieldValue('lastname', request.getParameter('lastname'));
    	customer.setFieldValue('companyname', request.getParameter('companyname'));
    	customer.setFieldValue('phone', request.getParameter('phone'));
    	customer.setFieldValue('email', request.getParameter('email'));
    	customer.setFieldValue('entitystatus', '6');    // LEAD::unqualified.
    	var customerId = nlapiSubmitRecord(customer, true);
	   
    	response.write(customerId);
    }
}

* Note: Unlike PHPToolkit Web Service API, Suitelets use setFieldValue() method to assign internalID of a Netsuite records.

3. You'll then have to create a Script Record. Script record associates the Suitelet javascript "function" with an external URL where general public can execute the script. To create a Script Record, go to Setup -> Customization -> Scripts -> New as shown on the diagram below.

4. To create a SuiteScript record, you'll have to fill in the form shown below. For SuiteScript type, you'll choose Suitelet and give a name of the Suitelet and the javascript function that will be invoked by the Script Record. If your Suitelet is dependent on another javascript (or Suitelet), you may add Library Script files under the "Libraries" tab.

5. As part of Script Record creation, you'll also create a deployment as screen shown below.

6. A completed deployment look something similar to below. Upon completing a Script Record creation, the NetSuite will create an external URL for you. This is the Scriptlet URL hit by the cURL calls. You'll have to give full permission to a User Role so that everyone can access the Suitelet that you have just deployed.

Comments

It should be noted that the Suitelet (and SuiteTalk for the same matter) uses a customform to update the records. So, it's important to assign the customform when inserting a record. If the customform being used does not have the valid select choices, for example entityStatus=6 (this is only available on Lead Forms, not on customer forms).

customer.setFieldValue('customform', 3); // Standard Lead form.

By Scott Seong (not verified)

While creating my first Suitelet to create a new customer, I've ran into a couple of problems. Here are the problems I've run into and the solutions.

1. Error #1: You do not have privileges to view this page

When the external Suitelet URL is clicked from the Netsuite Suitelet deployment page, the above error message is displayed. This is due to the Suitelet is not properly deployed with correct audiences.

To fix this problem, you'll have to modify the audiences. Make sure you do not choose any department or partners.

2. Error #2: Invalid entitystatus reference key 6.

Since we are creating a new customer of type Lead from an external website, we would like to create a customer with an entity status of Lead-Unqualified (Internal ID=6). However, whatever the reason the Suitelet wouldn't assign the correct entitystatus to the newly creating customer. Here is the code we've used.

function CreateCustomer(request, response)   {
	
    if (request.getMethod() == 'POST') {
    	var customer = nlapiCreateRecord('customer');
    	customer.setFieldValue('isperson', 'T');
    	customer.setFieldValue('customform', '3'); // Sales Lead Form
    	customer.setFieldValue('entitystatus', '6');    // LEAD::unqualified.
    	customer.setFieldValue('firstname', request.getParameter('firstname'));
    	customer.setFieldValue('lastname', request.getParameter('lastname'));
    	customer.setFieldValue('companyname', request.getParameter('companyname'));
    	customer.setFieldValue('phone', request.getParameter('phone'));
    	customer.setFieldValue('email', request.getParameter('email'));
    	var customerId = nlapiSubmitRecord(customer, true);
    }
}

To workaround this problem, we would create a customer with a default status (Customer-Closed Won) and then reload the customer for an update. Here is an updated Suitelet code that does the job we want.

function CreateCustomer(request, response)   {
	
    if (request.getMethod() == 'POST') {
    	var customer = nlapiCreateRecord('customer');
    	customer.setFieldValue('isperson', 'T');
    	customer.setFieldValue('customform', '3'); // Sales Lead Form
    	//customer.setFieldValue('entitystatus', '6');    // LEAD::unqualified.
    	customer.setFieldValue('firstname', request.getParameter('firstname'));
    	customer.setFieldValue('lastname', request.getParameter('lastname'));
    	customer.setFieldValue('companyname', request.getParameter('companyname'));
    	customer.setFieldValue('phone', request.getParameter('phone'));
    	customer.setFieldValue('email', request.getParameter('email'));
    	var customerId = nlapiSubmitRecord(customer, true);
    	var cust = nlapiLoadRecord("customer", customerId);
    	cust.setFieldValue('entitystatus', '6');
    	nlapiSubmitRecord(cust);
    }
}
By Scott Seong (not verified)

I had hell of time today trying to assign a Date/Time value to a custom entity variable. There is no documentation on how to do this on Netsuite or SERP on Google. I ended up finding an answer by trial-and-error after spending fair amount of time. The setFieldValue method takes a String value as an input, so the date/time value must be a String. I've tried a half dozen different formats including string conversions using the nlapiDateToString and nlapiStringToDate methods, but none of those worked.

2012-06-06 00:00:00 // SQL format
2012-06-06T00:00:00-06:00 // ISO 8601 format
06/06/2012 00:00:00 // Per user preferences setting
1339002061000 // In milliseconds

The solution was to add Ante meridiem (am) or Post meridiem (pm) value at the end of the format as shown below.

customer.setFieldValue('custentity_createdate', ‘06/06/2012 06:00:00 pm’);

It is worthwhile to use the UI form to manually insert a record, and it will help you identify the problem or hint you to a solution.

By Scott Seong (not verified)

Try using:

var customer = nlapiCreateRecord('customer', {stage: 'lead'});
customer.setFieldValue('entitystatus', '6');   

to create a customer in a Suitelet. It was verified working in my environment.

By Scott Seong (not verified)

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.