- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2024 10:26 PM
Hi ,
Requirement: Sc request raised by using Scripted rest api script and Variables also should be passed.
I am trying using to use out of the box — Buy Item(Service Catalog Api) Script in custom Scripted Rest resources but getting errors .
kindly help to create new custom api by using out of the box api script .
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2024 07:44 AM
Hi @naveenreddy0871 ,
you can use script like below. You need to query the record sys id and then pass.
var mgr = new GlideRecord('sys_user'); mgr.addQuery('name', manager); mgr.query(); if (mgr.next()) { var managerSysID = mgr.sys_id; cart.setVariable(item, "requested_for_manager", managerSysID); //here it is a reference field }
Please accept the solution if it helped.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2024 03:31 AM
Can you share the script you have created
Palani
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2024 04:05 AM
hi @naveenreddy0871 ,
I just tried to create a new Scripted rest service and resource ("Buy Item" from "Service Catalog API") as attached.
And I could create without any errors.
Is this what you want to do? or different?
If different, what kind of error and when do you face on errors?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2024 04:09 AM
Hi @naveenreddy0871 ,
You can follow below steps to create request.
- Create one scripted REST API.
- Create one resource with method POST.
- Script:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var obj = request.body.data;
var id = GlideGuid.generate(null); // Generate a unique GUID for the cart
var cart = new Cart(id);
var RITM = cart.addItem('774906834fbb4200086eeed18110c737'); // sys_id of the catlog item
// Set variables for the catalog item
cart.setVariable(RITM, 'group', obj.group); // set your variable
cart.setVariable(RITM, 'variable name', obj.variable name); // Set your variable
// Place the order
var rq = cart.placeOrder();
var responseBody = {};
responseBody.message = "Request created";
response.setBody(responseBody);
})(request, response);
For Testing, Go to REST API explorer
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2024 04:14 AM - edited 11-12-2024 04:18 AM
Thanks for your detailed explaination , I am using the below script for raising the request but unfortunately refrenced type of variable values are not passing into RITM. I am getting the variable values from the third party tool so there is no option to send the sys_id.
Scripted Rest API :