Issue in Populating response Body in ticket Creation AP

Vamshi_ch123
Tera Contributor

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."

Vamshi_ch123_0-1695027250981.png

 

 

(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);
1 ACCEPTED SOLUTION

Hi @Vamshi_ch123 

 

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);

 

AnveshKumarM_0-1695039292910.png

 

 

 

Thanks,
Anvesh

View solution in original post

9 REPLIES 9

AnveshKumar M
Tera Sage
Tera Sage

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 👍✔️

 

Thanks,
Anvesh

Hi @AnveshKumar M ,

 

I tried but getting below error

{
    "error": {
        "message""Script Evaluation Exception",
        "detail""Cannot convert {\"message\":\"Creation Success!\",\"detail\":\"Case CS0070692 created successfully\"} to org.mozilla.javascript.ScriptableObject (sys_ws_operation.2abdedb087bb25907d5032e83cbb358a.operation_script; line 27)"
    },
    "status""failure"
}

@Vamshi_ch123 

 

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);

 

Thanks,
Anvesh

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)

 

Vamshi_ch123_0-1695038421259.png