Follow @endyourif rss Facebook LinkedIn

Searching for and loading an invoice object using NetSuite Restlet Searching for and loading an invoice object using NetSuite Restlet

Published on Jan 18, 2020

This article assumes that you have some NetSuite experience and have created a NetSuite Restlet. I have written a previous article that describes how to authenticate with NetSuite for calling a Restlet using token based authentication that should help you get up-to-speed.

To search for objects using NetSuite's JavaScript Restlet implementation I will be using nlapiSearchRecord. Once the record has been found it will then be retrieved using nlapiLoadRecord.

To perform a search, you first need to define your filters (how you want to find your invoice) using nlobjSearchFilter. You will also need to define what columns should be returned in your search with nlobjSearchColumn. Let's take a look at an example:

var my_custom_field_id = 1;

var filters = [];
filters.push(new nlobjSearchFilter('my_custom_field', null, 'equalto', my_custom_field_id));

var columns = [];
columns.push(new nlobjSearchColumn('internalid'));

var searchResults = nlapiSearchRecord('invoice', null, filters, columns);

if (searchResults == null || searchResults == '' || searchResults.length == 0) {
return "Error, there's no invoice record corresponding to " + my_custom_field_id);

var invoiceId = searchResults[0].getId();

In this example there are a few hard-coded elements that would need updating to match your exact use-case. The first variable is my_custom_field_id, this is the value being used to find the invoice. I typically would pass this in as input to my Restlet call. The second is a magic string for a custom field that I have defined on the NetSuite invoice object called: my_custom_field.

These should be updated to match what you are searching.

Next up is creating the filter. Line 2 and 3 create a JavaScript array of filters and add one filter that will search for the custom field matching the input value. After this a columns array is created and I am asking for the NetSuite's internal ID of the invoice record.

These two variables then execute the aforementioned nlapiSearchRecord and stores in a variable. The results are then checked to see if any invoices were found. If none were found I've returned an error that can be read from the process calling the Restlet.

And finally, I store the internal ID into a variable that I will use in the next example to retrieve the invoice:

var invoiceId = searchResults[0].getId();

var invoiceRecord = nlapiLoadRecord('invoice', invoiceId);

var invoiceString = JSON.stringify(invoiceRecord);
nlapiLogExecution('DEBUG', "Invoice object:", invoiceString);

return invoiceRecord;

Using the ID retrieved earlier I execute the nlapiLoadRecord and store this in a variable. The next line is not required, but I like to trace out the results in NetSuite's debug log in case I need to inspect the results. And finally I return the full invoice record.

Similar to the error message, the process executing the Restlet call will now receive a JSON object of the NetSuite invoice.

Tags: NetSuite | nlapiLoadRecord | nlapiSearchRecord

My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP