1. Use search.lookupFields (Best for Fetching a Few Fields)
- When to use: You only need to retrieve a few fields from a record (without modifying it).
- Governance Cost: 2 units (compared to 10+ for
record.load).
- Limitation: Cannot access sublists or make updates.
- Example (Fetching Customer Name & Email)
let customerData = search.lookupFields({
type: search.Type.CUSTOMER,
id: 12345,
columns: ['entityid', 'email']
});
log.debug('Customer Name', customerData.entityid);
log.debug('Email', customerData.email);
2. Use search.create (For Bulk Lookups)
- When to use: Need to fetch multiple fields from multiple records efficiently.
- Governance Cost: 10 units per search execution (better than multiple
record.load).
- Limitation: Cannot modify records directly.
- Example (Fetching Email & Status for Multiple Customers)
let customerSearch = search.create({
type: search.Type.CUSTOMER,
columns: ['entityid', 'email', 'status'],
filters: [
['status', 'anyof', 'ACTIVE']
]
});
customerSearch.run().each(function(result) {
log.debug('Customer', result.getValue('entityid') + ' - ' + result.getValue('email'));
return true; // Continue iteration
});
3. Use N/query (For Large Datasets & Joins)
- When to use: Fetching data from multiple related records (like a SQL query).
- Governance Cost: 10 units per query execution (scalable for large data).
- Limitation: Read-only access, cannot update records.
- Example (Fetching Customers & Their Sales Reps Efficiently)
let query = query.runSuiteQL({
query: "SELECT id, entityid, email FROM customer WHERE isinactive = 'F'"
}).asMappedResults();
log.debug('Customers', query);
4. Use a Saved Search (If Reusable by Other Users/Scripts)
- When to use: If the lookup needs to be used in multiple scripts or UI dashboards.
- Governance Cost: Depends on usage, but avoids excessive script execution.
- Limitation: Needs to be maintained in NetSuite UI.
- Example (Loading a Predefined Saved Search in a Script)
let savedSearch = search.load({ id: 'customsearch_active_customers' });
savedSearch.run().each(function(result) {
log.debug('Customer Name', result.getValue('entityid'));
return true;
});
5. Use N/record.submitFields (For Direct Field Updates Without Loading)
- When to use: You need to update a few fields without loading the full record.
- Governance Cost: 10 units (same as
record.load, but avoids unnecessary data loading).
- Limitation: Cannot update sublists.
- Example (Updating a Customer’s Email Without
record.load)
record.submitFields({
type: record.Type.CUSTOMER,
id: 12345,
values: { email: 'newemail@example.com' }
});