🔹 Introduction
Two of the most commonly used modules in SuiteScript are:
N/record
→ Work with NetSuite records (create, load, update, delete).N/search
→ Find and retrieve data (saved search results, ad-hoc queries).
Together, these modules let you read and write records programmatically, and they form the foundation of most scripts.
🔹 The Record Module (N/record
)
Example 1: Create a Customer
What this does: Creates a new customer record and saves it.
/**
*@NApiVersion 2.1
*@NScriptType ScheduledScript
*/
define(['N/record'], (record) => {
const execute = () => {
try {
const customer = record.create({
type: record.Type.CUSTOMER,
isDynamic: true
});
customer.setValue({
fieldId: 'companyname',
value: 'Test Company Inc.'
});
customer.setValue({
fieldId: 'email',
value: 'testcompany@example.com'
});
const customerId = customer.save();
log.debug('Success', `Customer created with ID: ${customerId}`);
} catch (e) {
log.error('Error creating customer', e.message);
}
};
return { execute };
});
Example 2: Load & Update a Record
What this does: Loads an existing Customer by ID and updates its phone number.
/**
*@NApiVersion 2.1
*@NScriptType ScheduledScript
*/
define(['N/record'], (record) => {
const execute = () => {
try {
const customerId = 123; // replace with real internal ID
const customer = record.load({
type: record.Type.CUSTOMER,
id: customerId
});
customer.setValue({
fieldId: 'phone',
value: '555-123-4567'
});
customer.save();
log.debug('Success', `Customer ${customerId} phone updated.`);
} catch (e) {
log.error('Error updating customer', e.message);
}
};
return { execute };
});
Example 3: Delete a Record
/**
*@NApiVersion 2.1
*@NScriptType ScheduledScript
*/
define(['N/record'], (record) => {
const execute = () => {
try {
const deletedId = record.delete({
type: record.Type.CUSTOMER,
id: 123 // replace with real ID
});
log.debug('Success', `Customer ${deletedId} deleted.`);
} catch (e) {
log.error('Error deleting record', e.message);
}
};
return { execute };
});
🔹 The Search Module (N/search
)
Example 4: Load a Saved Search
What this does: Loads a pre-built saved search for Customers and logs results.
/**
*@NApiVersion 2.1
*@NScriptType ScheduledScript
*/
define(['N/search'], (search) => {
const execute = () => {
try {
const customerSearch = search.load({
id: 'customsearch_my_customer_search' // replace with your saved search ID
});
customerSearch.run().each(result => {
log.debug('Customer', `${result.getValue('entityid')} - ${result.getValue('email')}`);
return true; // continue to next row
});
} catch (e) {
log.error('Error loading saved search', e.message);
}
};
return { execute };
});
Example 5: Create an Ad-Hoc Search
What this does: Searches for active customers directly in script without a saved search.
/**
*@NApiVersion 2.1
*@NScriptType ScheduledScript
*/
define(['N/search'], (search) => {
const execute = () => {
try {
const customerSearch = search.create({
type: search.Type.CUSTOMER,
filters: [['isinactive', 'is', 'F']],
columns: ['entityid', 'email', 'phone']
});
const results = customerSearch.run().getRange({ start: 0, end: 10 });
results.forEach(result => {
log.debug('Customer', `${result.getValue('entityid')} | ${result.getValue('email')} | ${result.getValue('phone')}`);
});
} catch (e) {
log.error('Error creating search', e.message);
}
};
return { execute };
});
Example 6: Search + Update in One Script
What this does: Finds the first 10 inactive customers and reactivates them.
/**
*@NApiVersion 2.1
*@NScriptType ScheduledScript
*/
define(['N/search', 'N/record'], (search, record) => {
const execute = () => {
try {
const customerSearch = search.create({
type: search.Type.CUSTOMER,
filters: [['isinactive', 'is', 'T']],
columns: ['entityid']
});
const results = customerSearch.run().getRange({ start: 0, end: 10 });
results.forEach(result => {
const custId = result.id;
const customer = record.load({
type: record.Type.CUSTOMER,
id: custId
});
customer.setValue({
fieldId: 'isinactive',
value: false
});
customer.save();
log.debug('Updated', `Customer ${custId} reactivated.`);
});
} catch (e) {
log.error('Error reactivating customers', e.message);
}
};
return { execute };
});
🔹 Best Practices
- Use
record.submitFields()
for lightweight updates (faster than full load/save). - Use Saved Searches for complex filters you want non-devs to edit later.
- Always limit search results or use Map/Reduce for big datasets.
- Use try–catch +
log.error()
in every block for debugging.
✅ Key Takeaway
N/record
is your tool for CRUD operations (Create, Read, Update, Delete).N/search
lets you query records efficiently, either using saved searches or ad-hoc filters.- Together, these modules form the backbone of most SuiteScripts.