SuiteScript 2.0: Retrieving List/Record values returned by search.lookupFields

Remember when performing field look-up on a select type of field was as straightforward as this?

var recSO = nlapiLookupField("salesorder", soId, ['status'], false);
var soStatusValue = recSO.status;
var recSO = nlapiLookupField("salesorder", soId, ['status'], true);
var soStatusText= recSO.status;

You call the nlapiLookupField API and you can retrieve the values directly, just like this:

var soStatusValue = recSO.status;
var soStatusText= recSO.status;

You would notice that the API allows you to do this by setting the last parameter to either true or false. Depending on the parameter, you would either get the value or the text:

nlapiLookupField_screenshot

This is not ideal when you would want to get the text value of the List/Record field and want a combination of different field types.

In SuiteScript 2.0, value returned by the corresponding API will be in a JSON object with ‘value’ and ‘text’ as keys. This way, it is much easier to combine different field types in one lookup call.

Since the value is a JSON object, you need to retrieve the value by its index [0] and its key.  You can refer to the following snippet:

var soStatusValue = recSO.status[0].value;
var soStatusText = recSO.status[0].text;

Looking at the full code in debug mode:

require(['N/search'], function(search) {
var soId = 826;
var recSO = search.lookupFields({
type : "salesorder",
id : soId,
columns : ['status']
});
var soStatusValue = recSO.status[0].value;
var soStatusText = recSO.status[0].text;
})

Result would be this:

record.lookupFields_screenshot_SS2_0

Hope this helps. Feel free to leave any feedback on the Comments section below. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s