generating request from Schedule job

rahul67
Kilo Expert

i have been trying to generate Request ticket from Schedule Jobs using the following script

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
//gives sys_id of catalog item
var item = cart.addItem('5104596487ed0910d0d4ca27cebb35fc', 1);
item.setVariable("requested_for", gs.getUserID());
var rc = cart.placeOrder();

 

everything is works fine but not displaying the Requested_for value by default. Please help

 

find_real_file.png

1 ACCEPTED SOLUTION

I have written this in a Scheduled Job as you need, but in case you need to write or use this in an Server side function you can do it as well :

Scheduled Job screenshot for reference as well:

find_real_file.png

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

View solution in original post

23 REPLIES 23

Martin Ivanov
Giga Sage
Giga Sage

this is because gs.getUserID() is working for interactive sessions (where real users click in the system). Scheduled jobs have field Run as. you can set value there and use it through the current object or set a hardcoded value.

Please Mark Correct AND Helpful. Thanks!


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

can you please tell me how to set the field, 

on the scheduled job form - go to the context menu, configure->form layout. Then move the run as field from the left slushbucket to the right one.

Then you'll see it on the form. Just set some user there. In the script field you can see an example how to use it.

Mark Correct AND Helpful. Thanks!

find_real_file.png


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

I dnt know whether my question is clear or not.

i want to populate Requested For in the requested item form.

How to do it ?