- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 01:55 AM
Hello everyone,
I'm attempting to populate the response body with the ticket ID when a customer creates a ticket using the shared API. I've created a REST API script for this purpose, but I'm encountering an issue where I'm receiving a status code 200, but the response doesn't contain the ticket number or any accompanying message."
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) { this.status = '200'; var respObj = {}; var payload = request.body.data; var caller_id = payload.callerid; var environment = payload.Environment; var category = payload.Category; var priority = payload.Priority; var short_description = payload.Summary; var description = payload.description; var id = payload.W_Ticket_ID; var gr = new GlideRecord("sn_customerservice_case"); gr.initialize(); gr.short_description = short_description; gr.priority = priority; gr.category = category; gr.environment = environment; gr.short_description = short_description; gr.u_ticket_id = id; gr.description = caller_id + "\n" + priority + "\n" + environment + "\n" + short_description + "\n" + description + "\n" + id + "\n" + product + "\n" + account; gr.insert(); this.status = '200'; respObj.body = { "message": "Creation Success!", "detail": "Case " + gr.number + " created successfully" }; })(request, response);
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 05:15 AM
I tried the following code in my PDI and it is working fine (see the screen shot, please try this code for one more time.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
this.status = '200';
var respObj = {};
var payload = request.body.data;
var caller_id = payload.callerid;
var environment = payload.Environment;
var category = payload.Category;
var priority = payload.Priority;
var short_description = payload.Summary;
var description = payload.description;
var id = payload.W_Ticket_ID;
var gr = new GlideRecord("sn_customerservice_case");
gr.initialize();
gr.short_description = short_description;
gr.priority = priority;
gr.category = category;
gr.environment = environment;
gr.short_description = short_description;
gr.u_ticket_id = id;
gr.description = caller_id + "\n" + priority + "\n" + environment + "\n" + short_description + "\n" + description + "\n" + id + "\n" + product + "\n" + account;
gr.insert();
this.status = '200';
var case_number = gr.getValue('number') + '';
var resp_data_obj = {
message: "Creation Success!",
detail: "Case " + case_number + " created successfully"
};
return resp_data_obj;
})(request, response);
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 05:15 AM
I tried the following code in my PDI and it is working fine (see the screen shot, please try this code for one more time.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
this.status = '200';
var respObj = {};
var payload = request.body.data;
var caller_id = payload.callerid;
var environment = payload.Environment;
var category = payload.Category;
var priority = payload.Priority;
var short_description = payload.Summary;
var description = payload.description;
var id = payload.W_Ticket_ID;
var gr = new GlideRecord("sn_customerservice_case");
gr.initialize();
gr.short_description = short_description;
gr.priority = priority;
gr.category = category;
gr.environment = environment;
gr.short_description = short_description;
gr.u_ticket_id = id;
gr.description = caller_id + "\n" + priority + "\n" + environment + "\n" + short_description + "\n" + description + "\n" + id + "\n" + product + "\n" + account;
gr.insert();
this.status = '200';
var case_number = gr.getValue('number') + '';
var resp_data_obj = {
message: "Creation Success!",
detail: "Case " + case_number + " created successfully"
};
return resp_data_obj;
})(request, response);
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 05:34 AM
this worked thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 04:01 AM
you are writing the above code inside scripted rest api so it means ServiceNow will receive the request and will send response.
Are you sure the case got inserted? any BR is getting blocked?
try to use try catch block to see the exception
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
try{
this.status = '200';
var respObj = {};
var payload = request.body.data;
var caller_id = payload.callerid;
var environment = payload.Environment;
var category = payload.Category;
var priority = payload.Priority;
var short_description = payload.Summary;
var description = payload.description;
var id = payload.W_Ticket_ID;
var gr = new GlideRecord("sn_customerservice_case");
gr.initialize();
gr.short_description = short_description;
gr.priority = priority;
gr.category = category;
gr.environment = environment;
gr.short_description = short_description;
gr.u_ticket_id = id; gr.description = caller_id + "\n" + priority + "\n" + environment + "\n" + short_description + "\n" + description + "\n" + id + "\n" + product + "\n" + account;
gr.insert();
this.status = '200';
var resp_data = {
"message": "Creation Success!",
"detail": "Case " + gr.number + " created successfully"
};
response.setBody(JSON.stringify(resp_data));
}
catch(ex){
gs.info(ex);
}
})(request, response);
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 04:41 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 05:25 AM
what type of http method you created? post?
script looks good to me
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader