search.lookupFields Function

In NetSuite’s SuiteScript, the search.lookupFields function is used to perform a direct lookup of a record’s fields without having to load the entire record. This can be useful for retrieving specific field values quickly and efficiently.

Here’s a basic example demonstrating how to use search.lookupFields in SuiteScript 2.0:

/** 
* @NApiVersion 2.x 
* @NScriptType UserEventScript 
*/ define(['N/search'], 
function(search) { 

function beforeLoad(context) 
{ 
try 
{ 
// Define the record type and internal ID of the record you want to look up 
var recordType = 'customer'; // Replace with your record type 
var recordId = 123; // Replace with the internal ID of the record you want to look up 

// Define the list of fields you want to retrieve 
var fieldNames = ['entityid', 'companyname', 'email']; // Replace with your field names 

// Perform the lookup 
var lookupResult = search.lookupFields({ type: recordType, id: recordId, columns: fieldNames }); 

// Output the results to the log 
log.debug('Lookup Result', JSON.stringify(lookupResult)); 

// You can access individual field values from the lookup result 
var entityId = lookupResult.entityid;
 var companyName = lookupResult.companyname; 
var email = lookupResult.email; 
log.debug('Entity ID', entityId); 
log.debug('Company Name', companyName); 
log.debug('Email', email); 
} 
catch (e) 
{ 
log.error('Error in beforeLoad', e.toString()); 
} 
} 
return { beforeLoad: beforeLoad }; });

Explanation:

  1. Define the Script and Module: The script is defined as a User Event Script with the necessary SuiteScript 2.x module (N/search).
  2. Record Type and ID: Specify the type of the record (customer in this example) and the internal ID of the record you want to look up.
  3. Fields to Retrieve: Define an array of field names you want to retrieve.
  4. Perform Lookup: Use search.lookupFields to perform the lookup by providing the record type, record ID, and the field names.
  5. Log Results: Log the results to the NetSuite script logs for debugging purposes.
  6. Access Field Values: Access individual field values from the lookup result.

		

Leave a comment

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