'Undefined' Exception with RESTMessageV2 called from Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2015 04:48 PM
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');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-25-2015 05:36 PM
Hi Tessy,
Shouldn't you wait for the response to return in the second REST Message, before checking for the response code? If you don't, and you're using a MID Server to send the message, it will be undefined until the MID Server puts the response back into the ecc_queue and it gets processed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2015 02:28 PM
Thanks Cory.That made sense.I tried moving the 'getStatusCode' call below 'dresponse.waitForResponse(120);' as last in the 'try' block ,but I still get the 'undefined' Exception.Without 'getStatusCode' call ,I can get the responseBody
I tried logging the 'Response' object by JSUtil and it gives me an Exception
"com.glide.script.fencing.MethodNotAllowedException: Function RESTResponseV2 is not allowed in scope global
Caused by error in <refname> at line 33"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2015 02:43 PM
Hi Tessy,
I don't think we log Java objects with JSUtil anymore, across the board.
The fact that getting the status code on the second message fails is really odd, since it works fine on the first message.
Can you make the second call by itself? Run just the first SOAP call code, make sure it's response came back OK, then run the second one on it's own?
Is there any change in behavior when you do that?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2015 06:46 PM
Did try both the REST calls separately.No change in behavior.The first one logs the status code '200'.Second call when called separately does same as before. Goes to undefined exception.Tested the second call from the REST message.Gives a status '200'.