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:
- Define the Script and Module: The script is defined as a User Event Script with the necessary SuiteScript 2.x module (
N/search). - Record Type and ID: Specify the type of the record (
customerin this example) and the internal ID of the record you want to look up. - Fields to Retrieve: Define an array of field names you want to retrieve.
- Perform Lookup: Use
search.lookupFieldsto perform the lookup by providing the record type, record ID, and the field names. - Log Results: Log the results to the NetSuite script logs for debugging purposes.
- Access Field Values: Access individual field values from the lookup result.