i have a catalog form and when some one reassign the ticket , checking on the available stock first

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 . it is working on the check whether the next assignment_group have stockroom . 

 

chercm_0-1706884728148.png

 

 

chercm_1-1706884789522.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.getDisplayValue();

// 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.hasNext()) {
gs.addErrorMessage('No Stockroom found for the new Assignment Group. Assignment Group cannot be changed.');
current.setAbortAction(true);
return;
}

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

/
}

// Check alm_consumable table for items and quantities in the new assignment group
if (!isConsumableAvailable(newAssignmentGroup, items, quantities)) {
gs.addErrorMessage('Insufficient consumable items in the new Assignment Group. Assignment Group cannot be changed.');
current.setAbortAction(true);
return;
}
}
})(current, previous);

/
// Function to check consumable availability in alm_consumable table for items and quantities
function isConsumableAvailable(assignmentGroup, items, quantities) {
var consumableGR = new GlideRecord('alm_consumable');
consumableGR.addQuery('assignment_group', assignmentGroup);
 
// Assuming you have a field named "item" in your alm_consumable table
consumableGR.addEncodedQuery('itemIN' + items.getJournalEntry(-1));
consumableGR.query();

while (consumableGR.next()) {
var item = consumableGR.getValue('item');
var availableQuantity = consumableGR.getValue('quantity');

var requestedQuantity = parseInt(quantities.getElement(item).value);

if (requestedQuantity > availableQuantity) {
return false; // Insufficient consumable quantity
}
}

return true; // All consumables are available
}
0 REPLIES 0