- 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 02:25 AM - edited 09-18-2023 02:30 AM
Hi @Vamshi_ch123,
Try this,
(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 resp_data = {
"message": "Creation Success!",
"detail": "Case " + gr.number + " created successfully"
};
response.setBody(JSON.stringify(resp_data));
})(request, response);
And at the API consumption end parse the response back to JSON.
Please mark my answer helpful and accept as solution if it helped you to fix your issue 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 03:49 AM
Hi @AnveshKumar M ,
I tried but getting below error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 04:37 AM
Try the below code and see still you are getting an error, and check for the system logs if there is any error.
(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') + '';
try {
var resp_data_obj = {
message: "Creation Success!",
detail: "Case" + case_number + " created successfully"
};
var resp_body_string = JSON.stringify(resp_data_obj);
response.setBody(resp_body_string);
} catch (ex) {
gs.info("JSON Parse error: " + ex.message);
}
})(request, response);
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2023 05:00 AM
Hi @AnveshKumar M ,
Tickets are getting inserted, but response message is blank and in logs I could see the below message.
JSON Parse error: Cannot convert {"message":"Creation Success!","detail":"CaseCS0070699 created successfully"} to org.mozilla.javascript.ScriptableObject (sys_ws_operation.2abdedb08.operation_script; line 31)