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

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

this worked thank you

Ankur Bawiskar
Tera Patron
Tera Patron

@Vamshi_ch123 

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);
Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

Yes case getting inserted but the response message is not showing up.

@Vamshi_ch123 

what type of http method you created? post?

script looks good to me

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader