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

Hi CJB,

 

I am facing the similar issue. As you have menyioned here, How did you solve this. It works for me when I give the sys_id of the consumable record. But it is difficult to provide all the sys_id's of the consumable record.

 

Did you get any chance to find a solution for this?

 

Thanks,

Rini

Can you please let me know wt changes you made to the script, because me too facing the similar issue that instead quantity getting reduced a new empty record is getting created.

Thanks,

Rini

Hi @michael Ritchie,

Iam also facing same error, the quantity is consumed but it is not reduced rather a new empty record is getting created.

As Iam new to Assets and stocks, its really confusing to figure out the issue. It would be great if you can help me solve this issue.

Am I giving the right variable name?

Attached the workflow for your reference.

Best regards,

Rini

Hi Michael, 

The type of catalog item is hardware. It is a request. It's being used for a hardware request form that gets filled out by the procurer. Most of it is manual, but we wanted to have it being somewhat automatic for when an item is actually deducted from a stockroom.

rin1
Kilo Guru

Hi CJB,

I have got a similar requirement like this to reduce the quantity from stockroom. I also tried this script in my workflow(Runscript).

I have a doubt as Iam new to this, In the workflow where should we add the runscript. As of now I have added at the last. I have set like, When the request gets closed the runscript should run.

Once the request is closed A message is popped saying 'Stock is consumed' but then it is not getting reflected in the quantity.

It would be great if you could help me out.

 

Thanks,

Rini