I've created a script which should display an error message whenever user enter an invalid value and hit add/enter. I've used an event to cancel the request that is processing but still it's going to next screen. Below is the script, please look at it and let me know what else can I do to prevent it from going to next panel.
Thanks in Advance.
var MMS101_checkLotNumber = (function () {
function MMS101_checkLotNumber(scriptArgs) {
this.controller = scriptArgs.controller;
this.log = scriptArgs.log;
this.args = scriptArgs.args;
this.content = this.controller.RenderEngine.Content;
this.element = scriptArgs.elem;
this.host = scriptArgs.controller.ParentWindow;
var isTextChanging = false; // to avoid infinite loop in OnTextChanged
var OrderType = ""; //Order Type
var BANO = ""; //Lot Number
}
/**
* Script initialization function.
*/
MMS101_checkLotNumber.Init = function (args) {
new MMS101_checkLotNumber(args).run();
};
MMS101_checkLotNumber.prototype.run = function () {
try {
var key = this.scriptName;
var cache = InstanceCache;
ScriptDebugConsole.WriteLine("MMS101_checkLotNumber.prototype.run: Test");
if (!cache.ContainsKey(this.controller, key)) {
this.log.Info("Running...");
// Add a key to the instance cache to prevent other instances of this script on the same program instance.
cache.Add(this.controller, key, true);
}
ScriptDebugConsole.WriteLine("MMS101_checkLotNumber.prototype.run: OrderNumber = " + ScriptUtil.GetFieldValue("MRTRNR"));
// Attach events.
this.controller.Requesting.On(function (e) { this.onRequesting(e); });
this.controller.Requested.On(function (e) { this.onRequested(e); });
this.controller.RequestCompleted.On(function (e) { this.onRequestCompleted(e); });
} catch (ex) {
ScriptDebugConsole.WriteLine("MMS101_checkLotNumber.prototype.run: Error: Exception generated." + ex);
}
};
onRequesting = function (e)
{
var _this = this;
if(e.commandType === "KEY" && e.commandValue === "ENTER") {
ScriptDebugConsole.WriteLine("onRequesting: ENTER was pressed!");
ScriptDebugConsole.WriteLine("onRequesting: e.commandType=" + e.commandType);
ScriptDebugConsole.WriteLine("onRequesting: e.commandValue=" + e.commandValue);
try{
var _this = this;
var program = "MMS100MI";
var transaction = "GetHead";
ScriptDebugConsole.WriteLine("get_OrderType: Order = " + ScriptUtil.GetFieldValue("MRTRNR"));
var record = { TRNR: ScriptUtil.GetFieldValue("MRTRNR") };
var outputFields = ["TRTP"];
MIService.Current.execute(program, transaction, record, outputFields).then(function (response) {
//Read results here
this.OrderType = response.item.TRTP;
ScriptDebugConsole.WriteLine("onRequesting: OrderType = " + this.OrderType);
this.BANO = ScriptUtil.GetFieldValue("WRBANO");
ScriptDebugConsole.WriteLine("onRequesting: Lot Number = " + this.BANO);
if (this.BANO.indexOf("|") > 0 && this.OrderType == "QDI") {
ScriptDebugConsole.WriteLine("Init: OnTextChanged: WRBANO=" + ScriptUtil.GetFieldValue("WRBANO"));
ScriptDebugConsole.WriteLine("Lot Number index = " + this.BANO.indexOf("|"));
chk_BANO();
e.cancel = true;
}
}).catch(function (response) {
//Handle errors here
ScriptDebugConsole.WriteLine("MMS101_checkLotNumber: " + response.errorMessage);
});
} catch (ex) {
//Handle errors here
ScriptDebugConsole.WriteLine("MMS101_checkLotNumber: Error: Exception generated." + ex);
}
} else {
this.detachEvents();
}
}
onRequested = function (e)
{
ScriptDebugConsole.WriteLine("onRequested: ");
this.detachRequesting();
this.detachRequested();
controller.Requesting.Clear();
controller.Requested.Clear();
}
onRequestCompleted = function (e)
{
ScriptDebugConsole.WriteLine("onRequestCompleted: ");
controller.Requesting.off(function (e) { this.onRequesting(e); });
controller.Requested.off(function (e) { this.onRequested(e); });
controller.RequestCompleted.off(function (e) { this.onRequestCompleted(e); });
this.unregisterRequestCompleted();
}
function chk_BANO() {
try {
ScriptDebugConsole.WriteLine("chk_BANO: OrderType = " + this.OrderType);
this.BANO = ScriptUtil.GetFieldValue("WRBANO");
ScriptDebugConsole.WriteLine("chk_BANO: Lot Number = " + this.BANO);
ScriptDebugConsole.WriteLine("chk_BANO: Lot Number = " + this.BANO);
$("body").inforMessageDialog({
title: "Info",
shortMessage: "Incorrect Lot Number!" ,
detailedMessage: "Lot Number = " + this.BANO + ", cannot be added to this Order Type!",
dialogType: "Confirmation",
showTitleClose: false,
close: function (event, ui) {
$(this).remove();
},
buttons: [{
text: "OK",
click: function () {
$(this).inforDialog("close");
}, isDefault: true
}]
});
} catch (ex) {
//Handle errors here
ScriptDebugConsole.WriteLine("chk_BANO: " + ex);
}
}
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
return MMS101_checkLotNumber;
}());