- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2021 08:42 AM
Hi All,
I need to submit a catalog item based on the input parameters coming from a web page.
What I did till now:
1. Rest API explore --> Name space: sn_sc --> API Name: Service Catalog API --> entered the sys_id of the catalog item and given raw JSON body as below:
{
'sysparm_id': 'c28204ba2fe320100697bac62799b66d',
'sysparm_quantity': '1',
'variables':{
'short_description': 'check for api response',
'description': 'check for api response'
}
}
My doubts are:
- Sharing this endpoint to web developer is enough to get this done?
- Do I need to create any validation scripts?
- How to configure the outbound response, I want to show the RITM number instead of Request Number in webpage after submitting the request?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2021 01:12 AM
Hi,
you can share the details but the issue would be
1) consider your catalog item has variable which is of type reference to sys_user; so 3rd party won't know the user sys_id and it would lead to variable set to empty
So you better create scripted REST API and handle the request they send and then use Cart API to create RITM and REQ
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
// you can query sys_user and get the value and then set those reference variables
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);
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
‎10-11-2021 02:09 AM
Hi
When I try it in rest explorer, getting 500 Internal server error:
{
"error": {
"detail": "Cannot convert {\"status\":\"Error\",\"message\":\"Cannot convert {\\\"status\\\":\\\"Success\\\",\\\"requestNumber\\\":{},\\\"requestItemNumber\\\":{}} to org.mozilla.javascript.ScriptableObject (sys_ws_operation.c75dbdee078b3010f0ddfea89c1ed030.operation_script; line 43)\"} to org.mozilla.javascript.ScriptableObject (sys_ws_operation.c75dbdee078b3010f0ddfea89c1ed030.operation_script; line 50)",
"message": "Script Evaluation Exception"
},
"status": "failure"
}
I modified
response.setBody(JSON.stringify(res)); to response.setBody(res); and now it is working Ankur.
Working on this, will get back to you if I face any challenges.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2021 02:27 AM
Hi,
please share your script; some error in line 50
I hope you modified the script from your side based on your requirement.
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
‎10-11-2021 03:00 AM
Hi
Please find the below script.
I have few doubts:
- How to create multiple RITMs within the same request for the same catalog item and print those RITM numbers
- How to push multiple input values into list type variable?
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var reqbody = request.body.dataString;
try{
var parser = new global.JSON();
var parsedData = parser.decode(reqbody);
var shortdesc = reqbody.shortdescription;
var desc = reqbody.description;
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
// you can query sys_user and get the value and then set those reference variables
var item = cart.addItem('c28204ba2fe320100697bac62799b66d', 1);
//fill in the variables on the request item form
cart.setVariable(item,"short_description", shortdesc);
cart.setVariable(item,"description", desc);
var rc = cart.placeOrder();
// rc is the request number; you can query RITM table and get the RITM number
var reqNumber = rc.number;
var ritmNumber = '';
var ritm = new GlideRecord('sc_req_item');
ritm.get('request.number', reqNumber);
ritmNumber = ritm.number;
var res = {};
res["status"] = "Success";
res["requestNumber"] = reqNumber;
res["requestItemNumber"] = ritmNumber;
response.setBody(res);
}
catch(ex){
var res = {};
res["status"] = "Error";
res["message"] = ex.message;
response.setBody(res);
}
})(request, response);
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2021 03:14 AM
Hi,
Did I answer your original question?
If yes then please mark response as correct and helpful to close the question and the discussion can continue on same thread or separate thread.
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
‎10-11-2021 03:17 AM
I have marked as correct