Netsuite Suitelet Examples & Tutorials

NetSuite Suitelets plays vital part in integrating 3rd party web applications with the NetSuite. Writing a suitelet is as easy as writing a simple javascript except that it runs on server-side. Here is a couple of examples on how to write a suitelet. The first example creates a customer of a type LEAD-Unqualified from a POSTed web submissions.

function CreateCustomerRecord(request, response)   {
	
    if (request.getMethod() == 'POST') {
    	var customer = nlapiCreateRecord('customer', {stage: 'lead'});
    	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);
    }
}

The second example illustrates on how to retrieve a sales order from an order number, and update the record with new data. It is a bit complex than the first example, but the basic idea is the same. The sales order number is mapped to transId of the Sales Order Record, and we'll use it to find the internal ID of the sales order. Once we have the internalID of the sales order, updating a record is as easy as assigning the values and submitting a record.

function UpdateSalesOrder(request, response)   {
	
	if (request.getMethod() == 'POST') {
		
		// We'll first find the sales order that we need to update.
		var filters = new Array();
		filters[0] = new nlobjSearchFilter('tranId', null, 'is', request.getParameter('order_number') );
		 
		var columns = new Array();
		columns[0] = new nlobjSearchColumn( 'internalId' )
		
		var searchresults = nlapiSearchRecord( 'salesorder', null, filters, columns );
		if (searchresults != null && searchresults != '') {
			// We found an internal ID of the sales order, so this record must exist.
			var order = nlapiLoadRecord("salesorder", searchresults[0].getValue("internalId"));

			order.setFieldValue("shipdate", request.getParameter('ship_date'));
			order.setFieldValue("shipmethod", request.getParameter('ship_method'));

			nlapiSubmitRecord(order, true);
		}
	} else {
		response.write("Error: This page cannot be accessed directly.");
	}
}

Comments

I've made an attempt to create an ItemFulfillmentPackageList and ItemFulfillment via a Suitelet, but received the following error message.

Error: INVALID_RCRD_TYPE
The record type [ITEMFULFILLMENTPACKAGELIST] is invalid.

Here is the code snippet I used to create the records:

var filters = new Array();
filters[0] = new nlobjSearchFilter('tranId', null, 'is', '123');

var columns = new Array();
columns[0] = new nlobjSearchColumn( 'internalId' );
columns[1] = new nlobjSearchColumn( 'entity' );

var searchresults = nlapiSearchRecord( 'salesorder', null, filters, columns );
if (searchresults != null && searchresults != '') {
// We found an internal ID of the sales order, so this record must exist.
var orderInternalId = searchresults[0].getValue("internalId");

// Create a ItemFulfillmentPackageList Record
var ifpl = nlapiCreateRecord('itemfulfillmentpackagelist');
ifpl.setFieldValue('packageTrackingNumber', '1Z0000000000000000');
var ifpl_id = nlapiSubmitRecord(ifpl, true);

// Now, Create a ItemFulfillment Record
var iff = nlapiCreateRecord('itemfulfillment');
iff.setFieldValue('createdate', '2012-07-01');
iff.setFieldValue('entity', orderInternalId);
iff.setFieldValue('packagelist', ifpl_id);

nlapiSubmitRecord(iff, true);

Any idea why this is failing? Thanks.

By aladar

After a little more digging, and searching through the SuiteAnswer I was able to create an ItemFulfillment record (as shown on sample code below). However, the UPS tracking number inserted is not the one I added. Perhaps, transforming ItemFulfillment automatically create a tracking number??

var filters = new Array();
filters[0] = new nlobjSearchFilter('tranId', null, 'is', '123');
var searchresults = nlapiSearchRecord( 'salesorder', null, filters, null );
if (searchresults != null && searchresults != '') {
    var orderId = searchresults[0].getId();
    var iff = nlapiTransformRecord('salesorder', orderId, 'itemfulfillment');
    iff.setLineItemValue('package','packagetrackingnumber',1,'1Z0000000000000000');
    nlapiSubmitRecord(iff, true);
}
By aladar

Ok, got it figured out!!! It has to do with how "Shipping Items" are setup and whether the Shipping Item is integrated with the UPS. Uncheck the "Shipping Lael Integration" on the Shipping Item screen.

Then, the UPS Real-Time Rate / Flat Rate / Weight information has to be configured correctly, and depending on the setup you'll have supply the required list items through the API. Here is a modified script.

var filters = new Array();
filters[0] = new nlobjSearchFilter('tranId', null, 'is', '123');
var searchresults = nlapiSearchRecord( 'salesorder', null, filters, null );
if (searchresults != null && searchresults != '') {
    var orderId = searchresults[0].getId();
    var iff = nlapiTransformRecord('salesorder', orderId, 'itemfulfillment');
    iff.setFieldValue('shipmethod', '5'); // Some shipping method not integrated with UPS.
    iff.setLineItemValue('item','shipmethod',1,'5');  // Same Shipping method as Sales Order
    iff.setLineItemValue('package','packageweight',1, '1');  // If shipping cost is weight based.
    iff.setLineItemValue('package','packagetrackingnumber',1,'1Z0000000000000000');
    nlapiSubmitRecord(iff, true);
}

It should be noted that the Record Sublist is assigned with setLineItemValue() method as shown on the lines, 8 - 10.

By aladar

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.