The following script is executed after clicking a button on sales order. It creates work orders for items on the sales order if some conditions are fulfilled. It works fine, but sometimes it stops working and I need to undeploy and deploy the script again for it to work. Do you know what can cause this behaviour?
/**
* @NApiVersion 2.x
* @NScriptType workflowactionscript
*/
define(['N/record', 'N/search'],
function(record, search) {
var scriptName = "createWO";
"use strict";
function onAction (context) {
var funcName = scriptName + "onAction " + context.newRecord.type + " " + context.newRecord.id;
log.debug(funcName, "Starting");
try {
/**Getting values from SO.
*/
var currentRecord = context.newRecord;
var so = currentRecord.getValue({
fieldId: 'id'
});
var s = search.create({
type : search.Type.TRANSACTION,
columns:
[
search.createColumn({name: "item"}),
search.createColumn({name: "formulanumeric", formula: "({quantity}-NVL({quantitycommitted},0))-nvl({quantityshiprecv},0)"})
],
filters:
[
["type","anyof","SalesOrd"],
"AND",
["mainline","is","F"],
"AND",
["internalidnumber","equalto",so],
"AND",
["taxline","is","F"],
"AND",
["shipping","is","F"],
"AND",
["item.type","anyof","Assembly"],
"AND",
["formulanumeric: ({quantity}-NVL({quantitycommitted},0))-nvl({quantityshiprecv},0)","greaterthan","0"]
]
});
var searchResultCount = s.runPaged().count;
log.debug("s result count",searchResultCount);
s.run().each(function(result){
// .run().each has a limit of 4,000 results
return true;
});
var searchResult = s.run().getRange({start:0,end:999});
for(var j=0;j<searchResultCount;j++) {
var item = searchResult[j].getValue({name:'item'});
var qtytobuild = searchResult[j].getValue({name:'formulanumeric'});
var TO = record.create({
type: 'workorder',
isDynamic:true
});
TO.setValue({
fieldId: 'customform',
value: '50'
});
TO.setValue({
fieldId:'subsidiary',
value: 1
});
TO.setValue({
fieldId: 'location',
value: 2
});
TO.setValue({
fieldId: 'assemblyitem',
value: item
});
TO.setValue({
fieldId: 'quantity',
value: qtytobuild
});
var newWO = TO.save({
ignoreMandatoryFields: true
});
}
} catch (e) {
log.error(funcName + "Unable to create WO", e)
}
}
return {
onAction : onAction
};
});