I'm trying to write a script that will prevent finished goods from being withdrawn from inventory if the assembly build is awaiting approval. This is the first script I've ever done and first time ever using Javascript so I apologize if what I have so far makes no sense (and if I sound dumb). The script checks if the record is an assembly build that is being created or edited then creates a temporary field, "quantity pending". When saved, if approval status is "awaiting approval", the script assigns the value of quantity pending to the value in Quantity to Build, then Quantity to Build is set to 0 and the record is submitted. If the approval status is approved, Quantity to Build is set to the value stored in quantity pending, and quantity pending is set to 0, the record is then submitted. Again I apologize if this makes no sense.
I tried using the Script Debugger but I don't think my code works. Could someone point out where I went wrong or if I'm on the right track?
Here is what I have so far. If anyone has done something like this or knows of a better approach, I'm all ears.
function commitIfApproved() {
var cR = nlapiGetOldRecord();
var recordId = cR.nlapiGetRecordID();
var recordType = cR.nlapiGetRecordType();
if (recordId == 'assemblybuild') {
if(recordType = 'create' || 'edit') {
var approvalstatus = cR.nlapiGetFieldValue('approval status');
var quantityPending = cR.nlapiGetFieldValue('quantity to build - pending');
if (approvalstatus == "Awaiting Approval") {
cR.nlapiSetFieldValue('quantity to build', '0');
cR.nlapiSetFieldValue('quantity to build - pending', quantityPending);
cR.nlapiSubmitRecord();
alert("Finished goods will not be taken from inventory until this assembly build is approved.");
}
if (approvalstatus == "Approved") {
cR.nlapiSetFieldValue('quantity to build', quantityPending);
cR.nlapiSetFieldValue('quantity to build - pending', '0');
cR.nlapiSubmitRecord();
alert("This assembly build has been approved. Finished goods have been subtracted from inventory.");
}
}
}
}