Reputation: 43
I am trying to write a UserEvent script which prompts the user for a confirmation when they attempt to edit a sales order which has already had it's picking ticket printed. Below is my code:
define(['N/record', 'N/search', 'N/log', 'N/runtime', 'N/ui/dialog'], function (record, search, log, runtime, dialog) {
/**
*@NApiVersion 2.1
*@NScriptType UserEventScript
*/
var result = true;
function beforeSubmit(context) {
var order = context.oldRecord;
var orderStatus = order.getValue({fieldId: "status"});
if(orderStatus != "Billed") {
var orderInternalID = order.getValue({fieldId: "id"});
log.debug("id", orderInternalID);
var systemnoteSearchObj = search.create({
type: "systemnote",
filters:
[
["recordid","equalto",String(orderInternalID)],
"AND",
["field","anyof","TRANDOC.BPRINTEDPICKINGTICKET"],
"AND",
["newvalue","is","T"]
],
columns:
[
search.createColumn({
name: "record",
sort: search.Sort.ASC,
label: "Record"
}),
search.createColumn({name: "name", label: "Set by"}),
search.createColumn({name: "date", label: "Date"}),
search.createColumn({name: "context", label: "Context"}),
search.createColumn({name: "type", label: "Type"}),
search.createColumn({name: "field", label: "Field"}),
search.createColumn({name: "oldvalue", label: "Old Value"}),
search.createColumn({name: "newvalue", label: "New Value"}),
search.createColumn({name: "role", label: "Role"})
]
});
var searchResultCount = systemnoteSearchObj.runPaged().count;
log.debug("systemnoteSearchObj result count",Number(searchResultCount));
if(Number(searchResultCount) > 0) {
var options = {
title: 'WARNING: Pick Ticket Printed',
message: 'The pick ticket has already been printed for this sales order. Are you sure you want to edit?'
};
dialog.confirm(options).then(confirm).catch(cancel);
}
}
log.debug("result", result);
return result;
}
function confirm(reason) {
log.debug("User confirmed save.", reason);
result = true;
return true;
}
function cancel(reason) {
log.debug("User cancelled save.", reason);
result = false;
return false;
}
return {
beforeSubmit: beforeSubmit
}
});
When I deploy this script on the Edit event and try to edit and save the order, I get no confirmation dialog and instead receive the following error:
TypeError: dialog.confirm(...).then is not a function [at Object.beforeSubmit
. From what I've seen, this type of error occurs when you attempt to use .then() on a function which does not return a promise. However, in the documentation for dialog.confirm(), it clearly states that the function does in fact return a promise.
Why am I receiving this error and how can I avoid it to achieve the goal of the script?
Upvotes: 0
Views: 447
Reputation: 8847
A User Event script runs on the server side and thus cannot render UI components (there's no browser instance or window
object to render on). You'll likely need to transition this logic to the saveRecord
event of a Client Script.
Upvotes: 1