i tried to run a business rule to check availabilty in the stockroom but it is not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 06:04 AM - edited 02-06-2024 06:05 AM
Hi
i need some help . the below is not working for me
- Labels:
-
Walk-Up Experience
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2024 02:33 AM - edited 02-10-2024 02:35 AM
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2024 09:12 AM
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");
I hope it helps,
Kind Regards
Ashley
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 06:43 AM
what debugging have you done so far?
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2024 02:37 AM - edited 02-18-2024 03:15 AM
@Ankur Bawiskar i kept getting 0 in the logs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 06:48 AM
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