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.

Evaluation of error message table API

danielrueckert
Tera Contributor

Hello everyone,

I am currently familiarising myself a little with the subject of interfaces. I currently have an interface that creates an incident in another instance.

Instance 1 - Trigger Interface to post incident in Instance 2

The interface is also running smoothly. However, I am currently stuck with error handling.

With the variable


var resp = new JSONParser().parse(r.execute().getBody()).result;

 

I read the response of the interface and convert it back into javascript.
In the event of an error, I receive the response:

 

{

"error":{

   "message": "Operation Failed",

   "detail": "Operation against file 'incident' was aborted by Business Rule 'Abort changes on    

   group^e915f3b193870210a38a76718bba10dc'. Business Rule Stack:Abort changes on group"

   },

"status": "failure"

}

 

I would like to write the detail from this to the log. And try this with the variable

 

var responseIfError = resp.detail;

 

to read it out. However, the variable gets the value undefined.

When the interface runs through without errors, I read the incident number and system_id in the same way, which works without any problems.

 

var incidentID = resp.number;
var sysID = resp.sys_id;

 

Response shortened:

{

"result": {

   "parent":"",

   "made_sla":"true",

   "caused_by":"",

   "watch_list":"",

   "upon_reject":"cancel",

   "sys_updated_on":"2024-06-28 07:09:36",

   "child_incidents":"0",

   "hold_reason":"",

   "origin_table":"",

   "task_effective_number":"INC0010081",

   "approval_history":"",

   "number":"INC0010081",

   "sys_id":"0f73be3193430210a38a76718bba100c"

   }

}

 

Can you help me here and point out my error?

Thank you very much

1 ACCEPTED SOLUTION

That's because you go straight to the "result" property in this line of your code:

var resp = new JSONParser().parse(r.execute().getBody()).result;

 

But in case of an error, the top-level node of the response is called "error" and not "result". Hence the undefined value.

 

If you change the line above to this:

var resp = new JSONParser().parse(r.execute().getBody());

you should be able to retrieve the necessary information as follows:

// Traverse the "result" property in case of success
var incidentID = resp.result.number;
var sysID = resp.result.sys_id;

// Traverse the "error" property in case of error
var responseIfError = resp.error.detail;

View solution in original post

4 REPLIES 4

Slava Savitsky
Giga Sage

I think you need to use resp.error.detail instead of resp.detail

Unfortunately it does not work.

Thanks anyway

 

I have added the response in case of success.

That's because you go straight to the "result" property in this line of your code:

var resp = new JSONParser().parse(r.execute().getBody()).result;

 

But in case of an error, the top-level node of the response is called "error" and not "result". Hence the undefined value.

 

If you change the line above to this:

var resp = new JSONParser().parse(r.execute().getBody());

you should be able to retrieve the necessary information as follows:

// Traverse the "result" property in case of success
var incidentID = resp.result.number;
var sysID = resp.result.sys_id;

// Traverse the "error" property in case of error
var responseIfError = resp.error.detail;

It works. Thank you very much.👍