How to submit a catalog item from script?

karthikbabu
Giga Expert

Hi All,

 

I am trying to create an automatic ticket every month on 1st. The ticket should be created as a RITM which means it should be created from an existing catalog item (in our case "ADHOC item). it should follow the catalog item workflow. 

Please advise.

 

Thanks,

Karthik 

1 ACCEPTED SOLUTION

The cart API isn't instantiated via a GlideRecord. Also, you're not defining "adhoc" anywhere in your script. 

Try this:

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
 //add your requested item to the cart by sys_id of the catalog item
var item = cart.addItem('4054428fdb151f0097679ec6db9619c0', 1);

//fill in the variables on the request item form
cart.setVariable(item,"u_requested_for", "e80edd6edbec6bc097275e25ca9619a4"); 
cart.setVariable(item,"contact_number", "0");
cart.setVariable(item,"assignment_group", "87ec1c342b2e71406c487fb5a8da1524"); 
cart.setVariable(item,"application_service", "cdedfbcedb0f9744c291c170ba9619a7");
cart.setVariable(item,"short_description", email.subject);
cart.setVariable(item,"description", email.body_text);
var rc = cart.placeOrder();

 

After that, run your script to set your requested for values on sc_request or sc_cat_item (as you did in your example). A side note, you should revisit that GlideRecord. I don't know where you're getting "email.body_text" from (assuming you're running this on a schedule as you mentioned in your original post) and your variables and ritmSysID seem a little out of place, as you're not doing anything with them.

View solution in original post

10 REPLIES 10

The cart API isn't instantiated via a GlideRecord. Also, you're not defining "adhoc" anywhere in your script. 

Try this:

var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
 //add your requested item to the cart by sys_id of the catalog item
var item = cart.addItem('4054428fdb151f0097679ec6db9619c0', 1);

//fill in the variables on the request item form
cart.setVariable(item,"u_requested_for", "e80edd6edbec6bc097275e25ca9619a4"); 
cart.setVariable(item,"contact_number", "0");
cart.setVariable(item,"assignment_group", "87ec1c342b2e71406c487fb5a8da1524"); 
cart.setVariable(item,"application_service", "cdedfbcedb0f9744c291c170ba9619a7");
cart.setVariable(item,"short_description", email.subject);
cart.setVariable(item,"description", email.body_text);
var rc = cart.placeOrder();

 

After that, run your script to set your requested for values on sc_request or sc_cat_item (as you did in your example). A side note, you should revisit that GlideRecord. I don't know where you're getting "email.body_text" from (assuming you're running this on a schedule as you mentioned in your original post) and your variables and ritmSysID seem a little out of place, as you're not doing anything with them.

Thanks Matthew. This worked. I used the same script in Inbound Email and it worked. I thought it still works in Schedule Jobs but a slight change worked. 

Hi , 

 

How can I fill the "Requested for" referenced type filed value 

 

I have passed SYS_ID for this but not working. 

 

Thanks. 

I don't think you can directly set "Requested For" using the Cart Script Include mentioned in this thread. You can feed in a user, but it only sets the "Opened By". 

I think you have two choices:

  1. If you want to stick with the Cart Script Include, the ".placeOrder()" method returns an object of the REQ that's created, including the REQ's sys_id. You can use that in a GlideRecord to update the REQ with the appropriate user. This probably won't help all that much as the REQ workflow is already running, so your approvals would likely have been triggered by the time the update happens.
  2. Your other option is to use the CartJS API, which allows you to directly set the Requested For (this is the route I'd personally take)

 

Hope this helps

 

sachin_namjoshi
Kilo Patron
Kilo Patron

You do not need to write code for this requirement since you can use flow designer based on schedule to order catalog items.

 

https://docs.servicenow.com/bundle/london-servicenow-platform/page/administer/flow-designer/task/create-sc-flow.html

 

Regards,

Sachin