This script is to set the custom field batch number with the value in after submit that is the batch number should be in the format as (ACCOUNTNUMBER_TRANDATE)
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record', 'N/search', 'N/runtime', 'N/format'], function(record, search, runtime, format) {
function afterSubmit(context) {
try{
if (context.type !== context.UserEventType.CREATE) {
return;
}
var newRecord = context.newRecord;
var paymentDate = newRecord.getValue({ fieldId: 'createddate' });
log.debug('paymentDate',paymentDate);
var formattedPaymentDate = format.format({
value: paymentDate,
type: format.Type.DATETIME
});
var thirtySecondsAgo = new Date(paymentDate.getTime() - 30000);
var formattedThirtySecondsAgo = format.format({
value: thirtySecondsAgo,
type: format.Type.DATETIME
});
// Search for bill payments created within the last 30 seconds
var billPaymentSearch = search.create({
type: search.Type.VENDOR_PAYMENT,
filters: [
['createddate', 'within', formattedThirtySecondsAgo, formattedPaymentDate]
],
columns: ['internalid']
});
var searchResults = billPaymentSearch.run().getRange({
start: 0,
end: 1000 // Adjust the range as needed
});
// Generate a unique batch number
var batchNumber = 'BATCH-' + new Date().getTime();
// Update each bill payment with the generated batch number
searchResults.forEach(function(result) {
var paymentId = result.getValue({ name: 'internalid' });
record.submitFields({
type: record.Type.VENDOR_PAYMENT,
id: paymentId,
values: {
custbody_jj_batchnumber: batchNumber
}
});
});
log.debug('Batch Number Assigned', 'Batch Number: ' + batchNumber + ' has been assigned to ' + searchResults.length + ' bill payments.');
}
catch(e){
log.error('error@catch',e)
}
}
return {
afterSubmit: afterSubmit
};
});