'Undefined' Exception with RESTMessageV2 called from Script

tantony
Mega Guru

I have two outbound REST messages called from a scheduled script execution job using a MID server in Fuji.The 'Ok' response from first outbound REST call leads to the   second REST Message request.When tested in the individual REST message I could get the correct status code and response body.But when called from script the first call goes fine, but the second call fails with response.getStatusCode() causing 'undefined' exception.If I comment out that statement, I am able to get the   responseBody fine and able to parse and log it with JSUtil.logObject.Has anybody experienced this problem?

I have not been able to log the Response object by JSUtil.logObject or by XMLDocument2.I tried to turn on REST logging by setting the system property glide.rest.debug and also glide.rest.outbound.debug. But I don't see REST logs in system logs.How can I log the response object?

Hoping somebody can give me some direction.Below is the script I am using:

var now = new Date();

var day = now.getDay();

// do not run on saturday or sunday

if (day != 0 && day != 6) {

     

      // script

      try {

              var r = new sn_ws.RESTMessageV2('My_REST_message_1', 'get');

              r.setHttpTimeout(60000);

              var response = r.execute();

              response.waitForResponse(60);

              var responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();

              var httpStatus = response.getStatusCode();

              var parser = new JSONParser();

              var parsed = parser.parse(responseBody);

              gs.log('Response Status:' + parsed.status, 'My REST Message 1');

              global.JSUtil.logObject(parsed);

      }

      catch(ex) {

              var message = ex.getMessage();

              gs.log(message, 'My REST Message 1');

             

      }

      if (parsed.status == 'Ok'){// Make REST API calls to remote app's staging table and import the changes to imp_location table

              gs.log('Getting the daily changes from remote app..', 'My REST Message 2');

      try {

              var dr = new sn_ws.RESTMessageV2('My_REST_message2', 'get');

              dr.setHttpTimeout(80000);

              var dresponse = dr.execute();

              //var dhttpStatus = dresponse.getStatusCode(); //Commented out due to Exception

              //gs.log('Response Status Daily Changes:' + dhttpstatus, 'My REST Message 2');

              dresponse.waitForResponse(120);

              gs.log('Error status:' + dresponse.haveError() ,'My REST Message 2');

                     

              var dresponseBody = dresponse.haveError() ? dresponse.getErrorMessage() : dresponse.getBody();

              var parser = new JSONParser();

              var parsed = parser.parse(dresponseBody);

              global.JSUtil.logObject(parsed);

      }

      catch(ex) {

              gs.log('Daily Exception..', 'My REST Message 2');//This is logged if 'dresponse.getStatusCode()' statement is uncommented

              var message = ex.getMessage();

              gs.log('Daily Exception:'+message, 'My REST Message 2');//This is logged as 'undefined' if 'dresponse.getStatusCode()' statement is uncommented                           }

     

     

}else{

      if (parsed.status == 'Nil'){       //Data change is 0.00

              gs.log('No change to data to update', 'My REST Message 1');

     

     

}else{       //Data change greater than 10%, administrators notified and need manual intervention

            gs.log('Data change greater than 10%.Need Admin intervention.', 'My REST Message 1');

}

}

}

11 REPLIES 11

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Tessy,



That's weird. There is something wrong with the second call, and it doesn't really make sense. The objects you are using ate the same (RESTMessageV2), the methods return the same objects, etc.



I reformatted your code a little. I got confused in the try/catch and if/else block in the middle. Can you try this?



      // script


  var parser = new JSONParser();


  try {


              var r = new sn_ws.RESTMessageV2('My_REST_message_1', 'get');


              r.setHttpTimeout(60000);


           


              var response = r.execute();


              response.waitForResponse(60);


       


              var responseBody = response.haveError() ? response.getErrorMessage() : response.getBody();


              var httpStatus = response.getStatusCode();



              var parsed = parser.parse(responseBody);


              gs.log('Response Status:' + parsed.status, 'My REST Message 1');


              global.JSUtil.logObject(parsed);


      } catch (ex) {


              var message = ex.getMessage();


              gs.log(message, 'My REST Message 1');


      }



      if (parsed && parsed.status == 'Ok') { // Make REST API calls to remote app's staging table and import the changes to imp_location table


              gs.log('Getting the daily changes from remote app..', 'My REST Message 2');


              try {


                      var dr = new sn_ws.RESTMessageV2('My_REST_message2', 'get');


                      dr.setHttpTimeout(80000);


               


                      var dresponse = dr.execute();


                      dresponse.waitForResponse(120);


               


                      var dhttpStatus = dresponse.getStatusCode(); //Commented out due to Exception


                      gs.log('Response Status Daily Changes:' + dhttpstatus, 'My REST Message 2');


                      gs.log('Error status:' + dresponse.haveError(), 'My REST Message 2');



                      var dresponseBody = dresponse.haveError() ? dresponse.getErrorMessage() : dresponse.getBody();



                      var parsed = parser.parse(dresponseBody);


                      global.JSUtil.logObject(parsed);


              } catch (ex) {


                      gs.log('Daily Exception..', 'My REST Message 2'); //This is logged if 'dresponse.getStatusCode()' statement is uncommented


                      var message = ex.getMessage();


                      gs.log('Daily Exception:' + message, 'My REST Message 2'); //This is logged as 'undefined' if 'dresponse.getStatusCode()' statement is uncommented                           }


              }


      } else {


              if (parsed.status == 'Nil') //Data change is 0.00


                      gs.log('No change to data to update', 'My REST Message 1');



              else //Data change greater than 10%, administrators notified and need manual intervention


                      gs.log('Data change greater than 10%.Need Admin intervention.', 'My REST Message 1');


      }



I don't expect it to work, but then I don't expect you to run into the problems you are.


Tried that.Same behavior.The second call waits longer,for 13 seconds, to get the response.Thats the only obvious difference.Since I am getting the resposebody, I could parse it without a status code validation and hope to go from there.I am thinking of using this as a scheduled script execution job.


coryseering
ServiceNow Employee
ServiceNow Employee

Hi Tessy,



Super weird. I strongly recommend opening an Incident in Hi. Even if you can work around it, there seems to be an unresolved Problem here. If you open an Incident with steps to reproduce, the Support team can investigate more thoroughly, and possibly get the development team who works on that feature involved.



Thanks


Cory


silas1
ServiceNow Employee
ServiceNow Employee

Thanks Cory Seering for digging into this.


Tessy, can you try turning on the outbound debug property in sys_properties? (glide.rest.outbound.debug=true). That will give you quite a bit of detail about the request and response in the log. I'm also curious to see what the payloads from the ecc_queue look like. You can filter on agent 'RESTClient' in ecc_queue.


I did add glide.rest.outbound.debug=true in sys_properties before I started executing the script but I could not find any REST logs in System Log.I am on Fuji patch 4.I have come across couple of other people in the community saying that they were not able to see REST logs