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

Workflows are tricky because they are comprised of many records.  The easiest way is honestly to create a new update set, make it your current update set, checkout and publish the workflow without making changes and this will push it into your current set.

I was hoping it was a bug and it wasn't me. I tried on a personal instance and decided to see if even a simple version would work (like you did). I'm still getting consumed items sent to a new empty record like I posted above. I don't understand what I'm doing wrong. 😞

find_real_file.png

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Hum not sure what to say here.  Just to confirm and I am shooting in the dark here, are you converting the quantity to an integer?

var qty = parseInt(current.variables.quantity, 10);

 

Another shot in the dark...does the user who you are using to perform this action have the required roles to update those records?

Yes, they do. I'm just using system admin.

Could it possibly be the variables I'm pulling? For the Consumable dropdown, I am using Display Name. Is that correct or should it be using another field for the lookup? I tried to set everything up like Michael's screenshot. In the workflow properties, I have the table set to "Requested Item."

find_real_file.png