Hi there
I have a before record submit script that is supposed to run on a custom record and return an array of values called 'common values' if a value is found on more than one custom record:
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(["N/record", "N/search"], function (record, search) {
/**
* @param {UserEventContext.beforeSubmit} context
*/
function beforeSubmit(context) {
try {
var approverRecord = context.newRecord;
var returnType = approverRecord.getText({
fieldId: "custrecord_type_of_return",
});
log.debug("return type", returnType);
var returnAuthAppr = search.create({
type: "customrecord_ieturn_auth_approver",
filters: [],
columns: [
search.createColumn({ name: "internalid", label: "Internal ID" }),
search.createColumn({
name: "custrecord_first_approver",
label: "First Approver",
}),
search.createColumn({
name: "custrecord_second_approver",
label: "Second Approver",
}),
search.createColumn({
name: "custrecord_type_of_return",
label: "Type of Return",
}),
],
});
log.debug("search result object", returnAuthAppr.run());
var commonValues = [];
// Execute the search
returnAuthAppr.run().each(function (result) {
var searchReturn = result.getText({
name: "custrecord_type_of_return",
});
log.debug("searchReturn", searchReturn);
// Split the searchReturn into an array of values
var searchReturnArray = searchReturn.split(",");
log.debug("searchReturnArray", searchReturnArray);
// Loop through the returnType array
for (var i = 0; i < returnType.length; i++) {
// Loop through the searchReturnArray
for (var j = 0; j < searchReturnArray.length; j++) {
// Check if the current value from returnType exists in the searchReturnArray
if (returnType[i] === searchReturnArray[j]) {
commonValues.push(returnType[i]);
log.debug("common Values", commonValues);
break; // No need to continue comparing the same value
}
}
}
return true; // Continue processing more results
});
// Remove duplicates from the commonValues array
// commonValues = commonValues.filter(function (value, index, self) {
// return self.indexOf(value) === index;
// });
log.debug("Common Values", commonValues);
} catch (error) {
log.error("error", error.message + " " + error.lineNumber);
}
}
return {
beforeSubmit: beforeSubmit,
};
});
The getext method only works if I am editing an existing record.
On record creation, I get the following error:
Invalid API usage. You must use getValue to return the value set with setValue.
I am not setting any values with the script. The values are set by the user on the UI before pressing save.
Why would getText work correctly when editing and saving a record though not when creating it?
Thanks