- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2020 09:19 PM
Hello all!
I'm having some issue with parsing a JSON result from a POST call. I've perform the same test in POSTMAN with the same userr profile and I receive the following;
{
"import_set": "ISET1600403",
"staging_table": "u_test_request",
"result": [
{
"transform_map": "Test Transform",
"table": "sc_req_item",
"status": "error",
"error_message": "Target record not found",
"status_message": "Order Placed;Row transform ignored by onBefore script",
"test_sys_id": "6c6e2c55137688588013",
"test_display_value": "TEST0235449"
}
]
}
The particular chunk of my Business Rule is below;
var response = request.execute();
var parser = new JSONParser();
var parsedResponse = parser.parse(response);
gs.print(parsedResponse.import_set + "is the import set");
gs.print(parsedResponse.result + "is the .result");
gs.print(parsedResponse.result[0].test_display_value + "is the TEST DISPLAY VALUE");
gs.print(parsedResponse.result[0].test_sys_id + "is the TEST SYS ID");
However I only get back the following in the system log;
"org.mozilla.javascript.EcmaError: Cannot read property "0" from undefined"
I've also tried this for my Business Rule and received same error;
var parser = new JSONParser();
var responseBody = response.getBody();
var parameterArr = parser.parse(responseBody);
var responseSYSID = parameterArr.result[0].test_sys_id;
gs.print(responseSYSID);
I've used a nearly identical chunk of code in order to parse and retrieve the SYS ID and other data from the response when doing all of this to an incident table. Given the JSON in the response is identical I'm not sure why it's not working.
Thanks in advance!!!!!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2020 06:11 PM
Hi, I think that JSONParser() is legacy and so not available in a scoped app,
which will be why it works in a background script.
Maybe try something like
https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=JSONScopedAPI
var response = request.execute();
var parser = new global.JSON().decode(response.getBody());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2020 09:30 PM
Hi,
try running in scripts background once
I ran and it shows correctly
var str = '{"import_set":"ISET1600403","staging_table":"u_test_request","result":[{"transform_map":"Test Transform","table":"sc_req_item","status":"error","error_message":"Target record not found","status_message":"Order Placed;Row transform ignored by onBefore script","test_sys_id":"6c6e2c55137688588013","test_display_value":"TEST0235449"}]}';
var parser = new JSONParser();
var parsedResponse = parser.parse(str);
gs.info(parsedResponse.import_set + "is the import set");
//gs.info(parsedResponse.result + "is the .result");
gs.info(parsedResponse.result[0].test_display_value + "is the TEST DISPLAY VALUE");
gs.info(parsedResponse.result[0].test_sys_id + "is the TEST SYS ID");
Output is:
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2020 06:05 AM
Yes, it runs from the script background without issue.
If the script is proven to be functional, then as long as I'm passing in a "response" to begin with then it should work.
I know that the test_user has both read and write access to the endpoint and target table because I'm able to successfully see a response with all needed information.
Any ideas on why, when running via Business Rule, my response may be coming back Null/Blank?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2020 06:56 AM
Sorry, another key piece of information I should've mentioned is that I created an application in SNow specifically for this.
So these test requests are being generated inside of x_dev_test_requests custom application. I understand this may introduce issues of scope?
EDIT: My most recent Response Body according to logs;
{"error":{"message":"Exception while reading request","detail":"Cannot decode: java.io.StringReader@**body truncated**
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2020 08:32 AM
Hi Mike,
for scope app that will break; use this
var parser = new global.JSON();
var parsedData = parser.decode(str);
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader