Response was not getting as expected in scripted rest API

Sathwik1
Tera Expert

Response was not getting as expected in scripted rest API [Patch Rest API]

When I am using the below code, I am setting response as "Sysid" then it was perfectly fine...but when I was the send response as number and short description then I was getting error... what is the issue? how to set response as number and short description?

share the both codes.. please help me... It is a "Patch Rest API"

Below script, working fine [Response]

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var getID = request.pathParams.sysid;
    var grt = request.body.data;
    var gr = new GlideRecord('incident');
    gr.addQuery('sys_id', getID);
    gr.query();
    if (gr.next()) {
        gr.short_description = grt.short_description;
        gr.contact_type = grt.contact_type;
        gr.update();
		return getID;       //perfectly returning sysid
    }
})(request, response);

Below script, Issue with response

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
    var getID = request.pathParams.sysid;
    var grt = request.body.data;
    var gr = new GlideRecord('incident');
    gr.addQuery('sys_id', getID);
    gr.query();
    if (gr.next()) {
        gr.short_description = grt.short_description;
        gr.contact_type = grt.contact_type;
        gr.update();
    }
var n = gr.number;
response.setBody(n);
})(request, response);

@Ankur Bawiskar 

1 ACCEPTED SOLUTION

Raghu Ram Y
Kilo Sage

@Sathwik 

You are not setting response in the right format, so create a array and store all the variables in it then pass the array into the response, then it will work perfectly.

Hope it helps, if so please mark my response as correct and helpful.

View solution in original post

10 REPLIES 10

Prasad Pagar
Mega Sage

Hi @Sathwik 

Did you first check what you are getting in Reponse Body as complete data?

You need to parse through it and check Short Descr and Contact type.

Thank you
Prasad

Short description and contact type as updating..that was perfectly fine..I have verified.. In response I am getting 505 issue.. Can you suggest some other syntax for response?

Appli
Mega Sage
Mega Sage

Hi, where is a return statement for second script?

Probably you need something like

return {
        "number": gr.number,
        "short_description": gr.short_description,
};
Hope it helps

I Hope response or return works similarly..