Jaime Requena
–
January 3, 2023
User event scripts are commonly created to update data on a record before it is saved. This is referred to as “Before Submit”. But what if we want to have this logic happen on records which already exist? There are several approaches here. One is to create a CSV import of the records and update a field that is inconsequential to trigger the user event script. This may be okay, but you then have strange data on your records. Another approach is to do a mass update of the records and not update any fields. This will work but it will only trigger the user event script in the “XEDIT” context. In the “XEDIT” context, only the fields which are being updated are available unless you load the record. A third approach is to create a map/reduce script to load and save records based on a saved search. The loading and saving of the record will trigger user event scripts which execute using the “EDIT” context. I will detail this last approach.
As an example, I created a saved search of sales orders. Make sure the “Public” checkbox is checked. See below:

Notice this code assumes you are working with some type of transaction. This can easily be changed for other record types (Entity, Activity, etc.).
/**
* @NApiVersion 2.1
* @NScriptType MapReduceScript
* @NModuleScope SameAccount
*
*/
define([ 'N/runtime', 'N/record', 'N/search'],
(runtime, record, search) => {
getInputData = () => {
log.debug('===START===');
const scriptObj = runtime.getCurrentScript();
const savedSearch = scriptObj.getParameter({name: 'custscript_saved_search'});
const searchObj = search.load({
id: savedSearch
});
return searchObj;
}
map = (context) => {
try {
const fieldLookUp = search.lookupFields({
type: 'transaction',
id: context.key,
columns: ['recordtype']
});
const transRecord = record.load({
type: fieldLookUp.recordtype,
id: context.key
});
transRecord.save({
enableSourcing : true,
ignoreMandatoryFields : true
});
}
catch(e) {
log.error('Error loading and saving', JSON.stringify(e));
}
}
summarize = (summary) => {
log.debug('===END===');
}
return {
getInputData: getInputData,
map: map,
summarize: summarize
};
});

Once the script has finished processing, you will see that your user event script was triggered on each record.
Needing to trigger user event scripts on old records is a common request I run into. I hope you enjoyed reading about this solution. If you need help scripting or customizing NetSuite please contact Suite Tooth consulting here to set up a free consultation.
If you liked this article, please sign up for my newsletter to get these delivered to your inbox here.
Do not hesitate to contact us. We’re a team of experts ready to talk to you.