Help with error handling in Script Include

Abhijit Das7
Tera Expert

Hi Everyone,

 

I have created a custom Scripted REST API (POST Method), I am sending JSON data from scripted rest API to script include and updating work notes in incident. But I am facing in error handling.

 

Scripted REST API:

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var requestBody = request.body.dataString;
   var ship = new PostSpecialComments().addComments(requestBody, response);
})(request, response);

 

 

Script Include:

 

var PostSpecialComments = Class.create();
PostSpecialComments.prototype = {
    initialize: function() {},

    addComments: function(request, response) {
        // gs.info("Response = " + request);

        var parsedData = JSON.parse(request);   
        var table = parsedData.object_name;
        var sysId = parsedData.object_id;
       
        var responseBody = {};

        if (table == 'wm_order' || table == 'wm_task' || table == 'incident' || table == 'sn_customerservice_case' || table == 'pm_project') {

            var rec = new GlideRecord(table);
            rec.addQuery('sys_id', sysId);
            rec.query();
            if (rec.next()) {
               
    
                       // My logic of updating worknotes comes here.


                responseBody.status = "Success";
                responseBody.message = " ServiceNOW Operation performed successfully";
            } else {
                responseBody.status = "Failure";
                responseBody.message = "org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - incorrect value";
            }
        } else {
            responseBody.status = "Failure";
            responseBody.message = " org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - invalid table name:" + table + "(sys_ws_operation.526056a71b46d910a662ece5624bcbd5.operation_script; line 17)";

        }

		response.setBody(responseBody);
 
    },

    type: 'PostSpecialComments'
};

 

 

I am getting this error in REST API explorer when I test:

 

{
  "error": {
    "message": "The undefined value has no properties.",
    "detail": "ConversionError: The undefined value has no properties. (sys_script_include.b63853a247835a1061537e37536d4326.script; line 245)"
  },
  "status": "failure"
}

Basically, line 245 is response.setBody(responseBody). How can I solve my issue? Please help me in solving this issue.

 

Thanks in advance

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Abhijit Das7 

something like this

var PostSpecialComments = Class.create();
PostSpecialComments.prototype = {
    initialize: function() {},

    addComments: function(request, response) {
        // gs.info("Response = " + request);

        var parsedData = JSON.parse(request);
        var table = parsedData.object_name;
        var sysId = parsedData.object_id;

        var responseBody = {};

        if (table == 'wm_order' || table == 'wm_task' || table == 'incident' || table == 'sn_customerservice_case' || table == 'pm_project') {

            var rec = new GlideRecord(table);
            rec.addQuery('sys_id', sysId);
            rec.query();
            if (rec.next()) {


                // My logic of updating worknotes comes here.
                responseBody.status = "Success";
                responseBody.message = " ServiceNOW Operation performed successfully";
            } else {
                responseBody.status = "Failure";
                responseBody.message = "org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - incorrect value";
            }
        } else {
            responseBody.status = "Failure";
            responseBody.message = " org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - invalid table name:" + table + "(sys_ws_operation.526056a71b46d910a662ece5624bcbd5.operation_script; line 17)";

        }

        return JSON.stringify(responseBody);

    },

    type: 'PostSpecialComments'
};

Scripted REST API

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var requestBody = request.body.dataString;
   var ship = new PostSpecialComments().addComments(requestBody, response);
var parsedData = JSON.parse(ship); // conver string to object
response.setBody(parsedData); // set that as response body

})(request, response);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Abhijit Das7 

you should set response body etc inside your scripted rest api

the script include should simply update/add the work notes

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Abhijit Das7 

something like this

var PostSpecialComments = Class.create();
PostSpecialComments.prototype = {
    initialize: function() {},

    addComments: function(request, response) {
        // gs.info("Response = " + request);

        var parsedData = JSON.parse(request);
        var table = parsedData.object_name;
        var sysId = parsedData.object_id;

        var responseBody = {};

        if (table == 'wm_order' || table == 'wm_task' || table == 'incident' || table == 'sn_customerservice_case' || table == 'pm_project') {

            var rec = new GlideRecord(table);
            rec.addQuery('sys_id', sysId);
            rec.query();
            if (rec.next()) {


                // My logic of updating worknotes comes here.
                responseBody.status = "Success";
                responseBody.message = " ServiceNOW Operation performed successfully";
            } else {
                responseBody.status = "Failure";
                responseBody.message = "org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - incorrect value";
            }
        } else {
            responseBody.status = "Failure";
            responseBody.message = " org.mozilla.javascript.EvaluatorException: GlideRecord.addQuery() - invalid table name:" + table + "(sys_ws_operation.526056a71b46d910a662ece5624bcbd5.operation_script; line 17)";

        }

        return JSON.stringify(responseBody);

    },

    type: 'PostSpecialComments'
};

Scripted REST API

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    var requestBody = request.body.dataString;
   var ship = new PostSpecialComments().addComments(requestBody, response);
var parsedData = JSON.parse(ship); // conver string to object
response.setBody(parsedData); // set that as response body

})(request, response);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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