- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2020 08:29 AM
Hi,
I have a requirement to create RITM using scripted rest api. The values should be passed as json.
Any leads to do this would be highly appreciated.
TIA!
-Ruchi
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2020 08:58 PM
Hi Ruchi,
I believe you want to create a scripted REST API which would be consumed by 3rd party team. they would send information/variable data as json request corresponding to the variables you share with them. This will be only for single catalog item I consider
Example: if your catalog item has 4 variables as requested_for, requested_by, start_date, end_date
you can inform them to send data as below; so the json request would look like
{
"requested_for":"Abel Tuter",
"requested_by":"Fred Luddy",
"start_date":"2020-04-23",
"end_date":"2020-04-35"
}
Now example script in scripted rest API to create RITM for your catalog item with the above variable data
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var reqbody = request.body.dataString;
try{
var parser = new global.JSON();
var parsedData = parser.decode(reqbody);
var requestedFor = parsedData.requested_for;
var requestedBy = parsedData.requested_by;
var startDate = parsedData.start_date;
var endDate = parsedData.end_date;
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
// give here the sys_id of the catalog item under which those variables are present
var item = cart.addItem('4054428fdb151f0097679ec6db9619c0', 1);
//fill in the variables on the request item form
cart.setVariable(item,"requested_for", requestedFor);
cart.setVariable(item,"requested_by", requestedBy);
cart.setVariable(item,"start_date", startDate);
cart.setVariable(item,"end_date", endDate);
var rc = cart.placeOrder();
// rc is the request number; you can query RITM table and get the RITM number
var reqNumber = rc.number;
var ritm = new GlideRecord('sc_req_item');
ritm.get('request.number', reqNumber);
var ritmNumber = ritm.number;
var res = {};
res["status"] = "Success";
res["requestNumber"] = reqNumber;
res["requestItemNumber"] = ritmNumber;
response.setBody(JSON.stringify(res));
}
catch(ex){
var res = {};
res["status"] = "Error";
res["message"] = ex.message;
response.setBody(JSON.stringify(res));
}
})(request, response);
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2020 05:45 AM
Thank you Ankur for the help. It is working now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2020 09:23 PM
Hi Ruchi/ Ankur,
i am getting same error while creating ritm, how you resolved this error.
{
"error": {
"detail": "Cannot convert {\"status\":\"Error\",\"message\":\"Cannot convert {\\\"status\\\":\\\"Success\\\",\\\"requestNumber\\\":{},\\\"requestItemNumber\\\":{}} to org.mozilla.javascript.ScriptableObject (sys_ws_operation.5ca996601b0c2010868dddb6bb4bcb9b.operation_script; line 40)\"} to org.mozilla.javascript.ScriptableObject (sys_ws_operation.5ca996601b0c2010868dddb6bb4bcb9b.operation_script; line 47)",
"message": "Script Evaluation Exception"
},
"status": "failure"
}
Thank You!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2020 09:56 PM
Hi,
some issue in your scripted rest api script
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2022 03:05 AM
Hello
I have same requirement for ordering the item using scripted rest api.
When I am trying to post the data using json, the value for requested_for reference field is not showing the name its showing has empty.
If I am posting the data using sys_id of the user then it is taking up and showing the correct value.
Could you please help me on this issue, when i try to insert with name, then it will show the name of the user in reference field.
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2020 08:51 AM
Hello,
Full documentation from ServiceNow located here:
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!