
Anurag15
Giga Expert
Options
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 07-31-2022 09:32 AM
Here I have shown how to loop through all the variables of the Catalog Item and the Variable Set, put them in the JSON format and the do the POST using REST API. Then I have attached the Request and Response and Status Code to the RITM. This script can be re-used as it is in a run-script activity of the Workflow for the Catalog Item
var variablesArr=[];
var jsonObj={};
//See if there any variable Sets
var grVar1 = new GlideRecord("io_set_item");
grVar1.addQuery("sc_cat_item",current.cat_item.toString());
grVar1.query();
while(grVar1.next()){
//Get the Variables of the Variable Set
var grVar2=new GlideRecord("item_option_new");
grVar2.addQuery("variable_set",grVar1.variable_set.toString());
grVar2.query();
while(grVar2.next()){
variablesArr.push(grVar2.name.toString());
}
}
var grItem= new GlideRecord("item_option_new");
grItem.addQuery("cat_item",current.cat_item.toString());
grItem.query();
while(grItem.next()){
variablesArr.push(grItem.name.toString());
}
for(var i=0;i<variablesArr.length;i++){
jsonObj[variablesArr[i]]=current.variables[variablesArr[i]].toString()
}
//JSON.stringify(jsonObj);
//Post the JSON
//Can also call the Rest Message by using
//var r = new sn_ws.RESTMessageV2("rest_message_name");
var r = new sn_ws.RESTMessageV2();
r.setEndpoint("Endpoint")l;
r.setHttpMethod("POST");
r.setRequestHeader("Accept", "application/json");
r.setRequestHeader("Content-Type", "application/json");
r.setRequestHeader("x-api-key", "api_key");
r.setRequestBody(JSON.stringify(jsonObj));
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
//Attach the payload in RITM
var ResponseBody=JSON.stringify(jsonObj);
var httpStatus="200";
var grAttach = new GlideSysAttachment();
grAttach.write(current,"Payload.txt", "text", "RequestBody:\n\n"+JSON.stringify(jsonObj)+"\n\nResponseBody:\n\n"+JSON.stringify(jsonObj)+"\n\n"+"Status Code:"+httpStatus);
Thanks and Regards,
Anurag Mishra
Labels:
- 3,085 Views
Comments
Anish Sharma
Mega Explorer
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
07-31-2022
09:44 AM
This is good..!! Great way to dynamically loop through all the variables of a catalog item and build a JSON.