I keep getting 'null' and 'undefined' reading JSON responseBody in RESTmessage script

christaylor
Kilo Contributor

I have a Web Service Rest Message set up that works fine. In a scheduled script, I have this code:

var r = new sn_ws.RESTMessageV2('My Test Web Service', 'get');

var response = r.execute();

var responseBody = response.getBody();

var httpStatus = response.getStatusCode();

var parser = new global.JSON();    

var parsed = parser.decode(responseBody);

gs.log(parsed.entities[0].name);

I've tried a bunch of different things but I can never get it to print any of the elements of my decoded JSON string. I get this error:

org.mozilla.javascript.EcmaError: Cannot convert null to an object.

    Caused by error in <refname> at line 7

          4: var httpStatus = response.getStatusCode();

          5: var parser = new global.JSON();    

          6: var parsed = parser.decode(responseBody);

==>     7: gs.log(parsed.entities[0].name);

If I log "gs.log(parsed)" just to see what I get it says null.

I can't figure out what I'm doing wrong. I'm not even sure it's really sending a REST GET because I don't see anything in the ECC queue like I do if I run it from a workflow REST message activity. But, if I say "gs.log(response)" it logs   [object RESTResponseV2], so it looks like it's getting a response back. Here is what the response body is supposed to look like, tested using an API explorer on the web server:

{
  "metadata": {
  "grandTotalEntities": 2,
  "totalEntities": 2
  }
,
  "entities": [
  {
  "uuid": "dd0354e8-b691-470a-84b4-d78e64ff7e6a",
  "name": "CentOS-6.7-x86_64-bin-DVD1.iso",
  "annotation": "",
  "deleted": false,
  "containerId": 1072,
  "logicalTimestamp": 2,
  "imageType": "ISO_IMAGE",
  "vmDiskId": "4e1cd8c0-1f92-41cd-aa85-1d1e3d9cf8c3",
  "imageState": "ACTIVE",
  "createdTimeInUsecs": 1449270889358793,
  "updatedTimeInUsecs": 1449270889987846
  }
,
  {
  "uuid": "4c23affe-96ff-46e8-acc1-c6990ac3304b",
  "name": "CentOS-7-x86_64-DVD-1503-01.iso",
  "annotation": "",
  "deleted": false,
  "containerId": 1072,
  "logicalTimestamp": 3,
  "imageType": "ISO_IMAGE",
  "vmDiskId": "452e9fe8-a7bb-4ec0-abd4-86a71736c1a5",
  "imageState": "ACTIVE",
  "createdTimeInUsecs": 1450724128604482,
  "updatedTimeInUsecs": 1450735962991374
  }

  ]
}

9 REPLIES 9

Inacitve_User
Giga Contributor

Hi Syed,



Thanks of sending. I think here the problem is, chris is not getting the response back. I am aware of parsing. I think chris also tired the OOB Parser which you have pasted above. if not check the parsing given by syed.



Chris did you resolve the issue ?



Thanks,


Sujan


killswitch1111
ServiceNow Employee
ServiceNow Employee

Chris,



Can you log out the result of .getBody()? Also log out .getStatusCode() on the response object as well. You should get a 200.



IF the result of getBody looks correct then it may be a malformed issue of the JSOn. But I would want to be sure you are   getting anything back.


christaylor
Kilo Contributor

Yes I think my problem is I'm not getting anything back. I tried Sujan's suggestion to add a wait loop, or waitForResponse(), but whatever I do doesn't seem to make the script wait- I immediatly see it logging somethiing like 'null' or 'undefined' when I have a gs.log(response) or gs.log(parsed). When I log getBody() it's null, and logging getStatusCode() prints a 0, not a 200 like it should... I'm really not understanding why the script rushes to the end without waiting for a response no matter what I try. Is it something to do with running it as a scheduled job? This really shouldn't be so hard to do.



I know my restmessage works because I can run it from a workflow activity and see the response returned in the ECC queue log.


Inacitve_User
Giga Contributor

Chris,



How much time it is taking to receive the response using workflow. Try to check ecc queue time or workflow activity starts and end time. Please try to add same or more time to while loop and see. If still you are not getting anything back. Then there should some problem with the script which you are executing. Are you using midserver here ?



Thanks,


Sujan


Urgh! That's what it was- I had a MID server defined in my Web Service 'get' but it got deleted somehow. I added it back to the field and updated it and now the scheduled script runs fine. I don't even need to add in a wait or timeout loop- the HTTP response comes back right away. Thanks everyone for taking the time to look at this with me.