I would like to (eager) load a list of customers in netsuite that has been updated between 2 date/time range and have the results paginated.
I am relatively new to NetSuite SuiteScript 2.0, so I've implemented the lazy loading mvp version that works (without filtering) and it looks something like this:
define(['N/record', 'N/search'], function(record, search) {
function loadClients(context) {
var currencyMap = {};
var statusMap = {};
var results = [];
search.create({
type: search.Type.CUSTOMER,
// todo: Workout how to apply filter to load only customers updated between two date ranges (supplied via context) using 'lastmodifieddate'
}).run().getRange({
start: context.start || 0,
end: context.end || 100,
}).forEach(function(result) {
var customer = loadCustomer(result.id);
var currencyId = customer.getValue({ fieldId: 'currency' });
if (typeof currencyMap[currencyId] === 'undefined') {
currencyMap[currencyId] = loadCurrency(currencyId).getValue({
fieldId: 'name'
});
}
var statusId = customer.getValue({ fieldId: 'entitystatus' });
if (typeof statusMap[statusId] === 'undefined') {
statusMap[statusId] = loadStatus(statusId).getValue({
fieldId: 'name'
});
}
results.push({
tax_number: customer.getValue({ fieldId: 'vatregnumber' }),
name: customer.getValue({ fieldId: 'companyname' }),
first_name: '',
last_name: '',
updated_date: customer.getValue({ fieldId: 'lastmodifieddate' }),
has_attachments: '',
default_currency: currencyMap[currencyId],
is_supplier: 0,
contact_id: customer.id,
email_address: customer.getValue({ fieldId: 'email' }),
phones: customer.getValue({ fieldId: 'phone' }),
is_customer: 1,
addresses: customer.getValue({ fieldId: 'defaultaddress' }),
contact_status: statusMap[statusId],
});
});
return results;
}
function loadCustomer(customerId) {
return record.load({
type: record.Type.CUSTOMER,
id: customerId,
isDynamic: false
});
}
function loadCurrency(currencyId) {
return record.load({
type: record.Type.CURRENCY,
id: currencyId,
isDynamic: false
});
}
function loadStatus(statusId) {
return record.load({
type: record.Type.CUSTOMER_STATUS,
id: statusId,
isDynamic: false
});
}
return {
post: loadClients
}
});
As you can see, due to lack of knowledge of how this works, I am doing incredibly inefficient data loading and it's very slow. Takes approximately 1 minute to load 100 records.
Does anyone know how to achieve the above with filtering on lastmodifieddate
for the date/time range and eager loading correctly?