- 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-23-2020 08:50 AM
Hi Ruchi,
So what is your exact scenario? you want to create scripted rest api. what it should accept? Should it accept ritm variable values as json string
If yes then you need to parse the json and assign values in the script accordingly to respective variables
you can use below sample script in scripted rest api to create RITM for particular catalog item
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
// give here the sys_id of the catalog item
var item = cart.addItem('4054428fdb151f0097679ec6db9619c0', 1);
//fill in the variables on the request item form
cart.setVariable(item,"u_requested_for", "e80edd6edbec6bc097275e25ca9619a4");
cart.setVariable(item,"contact_number", "0");
cart.setVariable(item,"assignment_group", "87ec1c342b2e71406c487fb5a8da1524");
cart.setVariable(item,"application_service", "cdedfbcedb0f9744c291c170ba9619a7");
cart.setVariable(item,"short_description", "hello");
var rc = cart.placeOrder();
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-24-2020 06:14 AM
Hi Ankur,
Thanks for the response!
Yes my requirement is to create a RITM using the scripted rest api. The variables should be passed as json.
The above code when executed places an order for the item considering only the variables present in the catalog item
I want to pass variables from ritm and the request as well. like requested for,requested by etc..
- 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 04:44 AM
Hi Ankur,
When I am passing the variables in postman as below:
{
"acrobat":'true',
"Additional_software_requirements":"Test1"
}
P.S:Acrobat field is a checkbox
I am getting an error...
-----
var parser = new JSONParser();
var parsedResponse = parser.parse(reqbody);