The Zurich release has arrived! Interested in new features and functionalities? Click here for more

how to create request item from script

Alex_Rybalchenk
Tera Contributor

I'm trying to build a background script that will create a new request item. Since there are related tables involved, what would be the correct creation sequence, e.g. which table record should be created first to be associated with the request item record? If somebody has a working code snippet or a least an idea, I would appreciate it. Tried to find a solution in the community posts but related topics are not very descriptive.

13 REPLIES 13

bernyalvarado
Mega Sage

Hi Alex,



The best way to do it will be through the Service Catalog API


Service Catalog Script API - ServiceNow Wiki


Service Catalog API



Thanks,


Berny


Hey,

Are you talking about this ?

 

var cart = new Cart(null, gs.getUserID()); //Create the cart as the current user

var item = cart.addItem("sys_id of catalog item");

cart.setVariable(item, 'var name', 'value');

cart.setVariable(item, 'var name 2','other value');

var rc = cart.placeOrder();

 

If yes, then do we need to use cart.setVariable(item, 'var name', 'value'); for each variable of that particular catalog item? And what about the variable sets?

Please explain.

 

bernyalvarado
Mega Sage

Please also review the following blog post: Example of how cart() api could cause request items missing or in the wrong request



It's informative in regards how the system works. At the end it just reiterates how the cart should be generated according to the documentation:



var cartId = GlideGuid.generate(null);


var cart = new Cart(cartId);



Thanks,


Berny


Sakshi14
Giga Expert

Hey Alex,



Not very sure why you are trying to generate a request item through a background script, but wrt the order of records to be created, you need to create a request first, and then create a request item and associate it with the request (req_item.request = <sys_id> of request created. Assuming that the workflow will take care of the catalog tasks.



Another trivia that might come in handy for you is that the 'insert()' function actually returns the sys_id of the record being inserted, so you can use something like



var reqSysID = request.insert();


and then


req_item.request = reqSysID;


to establish the association.



Hope this helps!