how to create request item from script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2017 09:14 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2017 09:19 PM
Hi Alex,
The best way to do it will be through the Service Catalog API
Service Catalog Script API - ServiceNow Wiki
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 01:14 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2017 09:28 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2017 11:11 PM
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!