- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2018 03:33 PM
Hi, I'm trying to figure out a way to add a RunScript to a workflow (or another option) that allows for consumables to automatically be consumed.
We use a form that goes through a workflow.
I'm using this code that someone was very nice to upload on here, but I'm having trouble getting it to work properly:
var this_consumable = current.variables.alm_consumable;
//The this_consumable variable is a Reference variable used to select the consumable from the available stockrooms.
var this_qty = current.quantity;
var this_user = current.request.requested_for;
current.variables.alm_consumable_pull_from_stock = (" Qty "+ this_qty + "|" + this_consumable.getDisplayValue() );
//this line prints the value to a single line text variable on the Task form.
var new_sys_id = new Consumables().splitForeground(this_consumable, this_qty, '10', '', '', '', '', this_user);
//this is the script include that actually consumes and assigns to the user
Under consumables, the consumption is there, but it ends up looking like this. I need it to at least have the Display Name, if anything.
Thanks.
Solved! Go to Solution.
- Labels:
-
Enterprise Asset Management

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-08-2018 05:13 PM
Consuming a consumable should just decrement from the quantity of an existing consumable, not create a new record. Are you saying that it's creating a new record in your consumables table? If your script isn't working, it's probably because of one of four things (some of which are probably obvious to you but I'll include them anyway)...
1) The variable 'alm_consumable' isn't really referencing the 'alm_consumable' table
2) The variable 'alm_consumable' isn't really a reference variable
3) The variable 'alm_consumable' isn't really named 'alm_consumable'
If you take a screenshot of the back-end view of the 'alm_consumable' variable I can help you validate these.
4) Your run script in your workflow is right at the very beginning of your workflow. Sometimes the timing of the creation of the 'sc_req_item' record is such that the 'sc_request' record doesn't actually exist right at the exact second that the workflow kicks off. This would cause your 'this_user' variable to pick up an incorrect value because the request (and, in turn, the requested for) wouldn't be available yet.
I just tested the following script from the 'Scripts - Background' module in my own system and it ran successfully. You can use it to do a quick test independent of everything in your workflow script just to make sure the actual consumption is working like it should. Then you should be able to isolate to one of the 2 factors above (consumable ID or user ID) for sure. Just replace the sys_id in the first line with the sys_id of your consumable to test.
var this_consumable = '2357460637732000158bbfc8bcbe5d0e';
var this_qty = '3';
var this_user = gs.getUserID();
var new_sys_id = new Consumables().splitForeground(this_consumable, this_qty, '10', '', '', '', '', this_user);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2021 12:23 AM
hi Rin,
So you need to get the sys_id in a variable, same is mentioned in code snippet.
var consumableAsset = current.variables.consumable;
Lets say your consumable asset is stored in this variable called consumable so as per above line this set it in 'consumableAsset' variable and every time dynamically it will take that asset sys_id which you do not need to hardcode.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2021 03:11 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2021 03:55 AM
Can you paste here Runscript?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2021 03:58 AM
I tired both the below scripts:
var consumableAsset = current.variables.consumable;
var qty = parseInt(current.variables.quantity, 10);
var requestedFor = current.variables.requested_for;
new Consumables().splitForeground(consumableAsset, qty, '10', '', '', '', '', requestedFor);
var this_consumable = current.variables.alm_consumable;
var this_qty = current.quantity;
var this_user = current.request.requested_for;
current.variables.alm_consumable_pull_from_stock = (" Qty "+ this_qty + "|" + this_consumable.getDisplayValue() );
var new_sys_id = new Consumables().splitForeground(this_consumable, this_qty, '10', '', '', '', '', this_user);
Thanks,
Rini
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-27-2021 04:28 AM
So this is tested by me on the instance, now you need to check if you have this script include in your system:
https://yourinstance.service-now.com/nav_to.do?uri=%2Fsys_script_include_list.do%3Fsysparm_query%3Dname%3DConsumables%26sysparm_first_row%3D1%26sysparm_view%3D
Also the variable name you need to check, because in both the script there are different variable name. So check if the Consumable asset variable name is the one which you have mentioned in the script. Also both the scripts are same just label change. So make sure you following the correct variable name here:
var this_consumable = current.variables.alm_consumable;
Also are you checking the alm_consumable table where the exact quantity is getting reduced for validation purpose.
now here is the working one in my system:
below is the reference field referencing the alm_consumable table which will allow you to select the available asset.
now after running the script, validate the quantity field from below link:
https://yourinstance.service-now.com/alm_consumable_list.do?sysparm_query=&sysparm_first_row=1&sysparm_view=