/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/record', 'N/error', 'N/email', 'N/render', 'N/runtime', 'N/ui/serverWidget', 'N/search'], function(record, error, email, render, runtime, ui, search) {
function beforeSubmit(context) {
if (context.type === context.UserEventType.CREATE || context.type === context.UserEventType.EDIT) {
var noClass = false;
var listClassCount = context.newRecord.getLineCount({ sublistId: 'item' });
var hasNoClass = [];
for (var i = 0; i < listClassCount; i++) {
var itemId = context.newRecord.getSublistValue({ sublistId: 'item', fieldId: 'item', line: i });
var itemClass = search.lookupFields({ type: 'item', id: itemId, columns: 'class' });
if (!itemClass.class) {
noClass = true;
hasNoClass.push(context.newRecord.getSublistText({ sublistId: 'item', fieldId: 'item', line: i }));
}
}
if (noClass) {
var errorMessage = 'The following items do not belong to any class: ' + hasNoClass.join(', ') + '. Kindly check the following items.';
throw error.create({ name: 'NOCLASSITEM', message: errorMessage, notifyOff: true });
}
}
}
function afterSubmit(context) {
if (context.type === context.UserEventType.CREATE || context.type === context.UserEventType.EDIT) {
var currentRecord = context.newRecord;
var salesOrderId = currentRecord.id;
// Rest of the code for sending email and adding button remains unchanged
}
}
return {
beforeSubmit: beforeSubmit,
afterSubmit: afterSubmit
};
});