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

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

Thank you for being so patient with me. I'm just getting started with ServiceNow. I see that it's now showing sys_ids in the dropdown. After doing some research, it appears that I have to put each label in for the consumables? Making a change to the display setting in the dictionary may have bad consequences? 

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

What type of variable is this?  It should only allow one selection right?  If so it needs to be of type reference.

That did the trick! Thanks so much! I'm definitely going to do some more reading on SN. I was kinda just thrown into SN, so I'm learning slowly.

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Awesome!  Glad you got it working.  I should have asked about that earlier but regardless glad you got it working.

 

Please mark any of these posts as helpful or the correct answer to your question so others viewing may benefit.