i tried to run a business rule to check availabilty in the stockroom but it is not working

chercm
Mega Sage

Hi 

 

i need some help . the below is not working for me 

 

// 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();
gs.info('Number of records: ' + consumableGR.getRowCount());
 
// 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);
 
chercm_0-1707228322935.png

 

 
 
9 REPLIES 9

@Anurag Tripathi @Ankur Bawiskar 

 

i tried with this too : gs.info(current.u_stockroom); 

 

not getting anything in the logs  but the field is filled 

 

chercm_0-1707561317845.png

 

Hi @chercm 

 

I hope you're doing well,

 

I just wanted to throw in a quick pro tip when doing debugging as its quite hard viewing the screen shot above.

 

I find it easier to use gs.log instead gs.info as you can add an optional parameter at the end in quotes which will update the Source column within the logs. Makes it much easier to filter on and follow each output i.e. Step 1, Step 2. etc

 

Example: gs.log("Adding a log", "Ashley");

 

Screenshot 2024-02-21 171012.png

I hope it helps,

 

Kind Regards

 

Ashley

Ankur Bawiskar
Tera Patron
Tera Patron

@chercm 

what debugging have you done so far?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar i kept getting 0 in the logs 

 

(function executeRule(current, previous /*null when async*/)
{
var requestedItem = current.request_item.getRefRecord();
 

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 = requestedItem.variables.item_requested; // Assuming "item" is the variable name for items
var req_quantity = +requestedItem.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();
gs.info('Number of records: ' + consumableGR.getRowCount());
gs.info(current.u_stockroom);
gs.info("Items requested: " + items);
gs.info("Requested quantity: " + req_quantity);
 
// 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);
 
 
chercm_0-1708254950572.png

 

Screenshot 2024-02-18 at 6.40.01 PM.png

Mark Manders
Mega Patron

What are your logs giving back? Which part of the rule isn't working? Your 'Assuming that....' in your code, gives me the impression that this code is coming from some kind of AI?

 

It could be that calculations aren't working because in variables, they are strings, but I'm not sure if that's the part that isn't working, since you only posted the script, without any further information. If it is the calculation, maybe this will work:

(function executeRule(current, previous /*null when async*/) {

    var items = current.variables.item_requested;
    var req_quantity = parseInt(current.variables.quantity, 10); // Convert quantity to integer

    var consumableGR = new GlideRecord('alm_asset');
    consumableGR.addQuery('install_status', 6);
    consumableGR.addQuery('stockroom', current.u_stockroom);
    consumableGR.addQuery('sys_id', items);
    consumableGR.query();
    gs.info('Number of records: ' + consumableGR.getRowCount());

    if (consumableGR.next()) {
        var availableQuantity = parseInt(consumableGR.quantity, 10); // Assuming 'quantity' is the correct field and converting it to integer

        try {
            var quantity = availableQuantity - req_quantity;
            if (quantity >= 0) {
                // If quantity is sufficient, do nothing or perform further actions as needed
                gs.info('Sufficient quantity available for the item: ' + items);
            } else {
                gs.addErrorMessage('Insufficient consumable items for item: ' + items + '. Available quantity: ' + availableQuantity + ', Requested: ' + req_quantity);
                current.setAbortAction(true);
            }
        } catch (ex) {
            gs.addErrorMessage('Unexpected Error: ' + ex.message);
            current.setAbortAction(true);
        }
    } else {
        gs.addErrorMessage('Item not found or not available in the specified stockroom.');
        current.setAbortAction(true);
    }

})(current, previous);

 
If something else isn't working, please provide that information in your question. That helps in solving something without having to make too many assumptions.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark