Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

'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

Was able to look at the ECC payload.For both REST calls the payload looked the same.Both had <parameter name="http_status_code" value="200"/>


You may want to try to convert the response to string first (responseBody.toString()), maybe that will help