Script works in 'Scripts - Background' module, but not workflow?

CJB
Tera Expert

Hello, everyone, I am working on a script that automatically consumes. It works fine when I use the Script - Background module, but when I use it in a workflow, I get a different result. Basically, in the workflow, it takes the consumable and puts it in both Consumed and In Use state with an empty Display Name and Model. 

Before running the workflow, the top row was 60. I entered a quantity of 2 and it got sent to the two top rows. 

find_real_file.png

I just want it to show up as the actual item I selected. I am open to another way to automatically consume. I just need it to work with the workflow. 

Here is the script:

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

 

Thank you,

CJB

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

With this script, consumableAsset must be the SysID of the consumable not the display value:

new Consumables().splitForeground(consumableAsset, qty, '10', '', '', '', '', requestedFor);

View solution in original post

24 REPLIES 24

CJB, I created a simple catalog item in my personal dev instance:

find_real_file.png

I have a simple workflow that has a run script to launch the code below.

find_real_file.png

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);

It works fine in this instance.

Thanks, Michael. I am going to try testing this right now. 

Hey Michael, 

I just tried the script. It is getting triggered in the workflow, but it continues to be added in a new, empty consumed consumable. Is there another change that I need to make in the workflow?

find_real_file.png

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

What release are you on?  Wonder if there is a bug.  My PDI is Kingston Patch 6 as an example.

Currently, my company is on Jakarta (we plan on upgrading in a few months). 

Is there a good way of copying over this workflow to my personal instance? I want to see if I can get it working on that. Thanks.