Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to check on available of items in the stock room before allowed to reassign ticket ?

chercm
Mega Sage

Hi i have a catalog form that needs to check on the next available stocks for the next stock room before reassigning the ticket successfully else to throw an error. 

 

the code below i tried and it is not working on the check for the availability of the stock in the re-assigned group .

 

chercm_0-1706944341231.png

 

 

 

chercm_1-1706944341232.png

 

 

 

(function executeRule(current, previous /*null when async*/) {
// Check if the assignment_group field is being modified
if (current.assignment_group.changes()) {
// New assignment group value
var newAssignmentGroup = current.assignment_group;

// Query the Stockroom based on the new Assignment Group
var stockroomGR = new GlideRecord('alm_stockroom');
stockroomGR.addQuery('assignment_group', newAssignmentGroup);
stockroomGR.query();

// If no stockroom is found, prevent the assignment group change
if (!stockroomGR.next()) {
gs.addErrorMessage('No Stockroom found for the new Assignment Group. Assignment Group cannot be changed.');
current.setAbortAction(true);
return;
} else {
current.setAbortAction(false);
return;
}
}

// Check items and quantities in variables
var items = current.variables.item_requested; // Assuming "item" is the variable name for items
var req_quantity = current.variables.quantity; // Assuming "quantity" is the variable name for quantities

var consumableGR = new GlideRecord('alm_asset');
consumableGR.addQuery('install_status', 6);
consumableGR.addQuery('stockroom', current.u_stockroom);
consumableGR.addQuery('sys_id', items);
consumableGR.query();

// Check consumable availability for each item
if (consumableGR.next()) {
var availableQuantity = consumableGR.getValue('quantity');
try {
var quantity = availableQuantity - req_quantity;
if (quantity >= 0) {
current.setAbortAction(false); // Adjusted to false if quantity is sufficient
return;
} else {
gs.addErrorMessage('Insufficient consumable items for item: ' + items + ' in the new Assignment Group. Assignment Group cannot be changed.');
current.setAbortAction(true);
return;
}
} catch (ex) {
gs.addErrorMessage('Unexpected Error: ' + ex.message);
current.setAbortAction(true);
return;
}
}
})(current, previous);
0 REPLIES 0