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

Prateek kumar
Mega Sage

How about you use a Scheduled job to fireoff this ticket creation?


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Matthew Glenn
Kilo Sage

Create a scheduled job that runs a script on the 1st, then utilize the Cart API

Hi Mathew,

 

I have followed your suggestion and built out below script but somehow it is not creating record.

 

var gr = new GlideRecord('sc_cart');
//add your requested item to the cart by sys_id of the catalog item
var item = adhoc.addItem('4054428fdb151f0097679ec6db9619c0', 1);


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

var ritmSysID = "";
var ritmRec = new GlideRecord("sc_req_item");
ritmRec.addQuery("request", rc.sys_id);
ritmRec.query();
if(ritmRec.next()){
var a = ritmRec.sys_id;
ritmSysID = a;
ritmRec.u_requested_by = "e80edd6edbec6bc097275e25ca9619a4";
ritmRec.work_notes = email.body_text;
ritmRec.update();
}

 

Please advise.

Thanks,

Karthik

Are you writing the script in inbound action ?

You are missing the cart declaration

Try this script

var cartId = GlideGuid.generate(null);
var adhoc = new Cart(cartId);

var item = adhoc.addItem('4054428fdb151f0097679ec6db9619c0', 1);



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


var ritmSysID = "";
var ritmRec = new GlideRecord("sc_req_item");
ritmRec.addQuery("request", rc.sys_id);
ritmRec.query();
if(ritmRec.next()){
	
	ritmRec.u_requested_by = "e80edd6edbec6bc097275e25ca9619a4";
	ritmRec.work_notes = email.body_text;
	ritmRec.update();
}