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.

Extracting field from response body gives undefined

AdrianHolm
Tera Guru

Hello,

I have an issue regarding extracting field from a response body, which currently is giving me undefined value in background scripts. Here's how the script and response looks;

var response = request.execute();
var responseBody = response.getBody(); 
gs.print(responseBody); //This gives me [{"error":"SIM already activated for MSISDN 1122334455667788","ret":-2}]

var parse = JSON.parse(responseBody);
gs.print(parse); //This gives me [object Object]

var errorMessage = parse.error; //Here's where I'm trying to extract 'error' message from responseBody, but doesn't seem to be correct
gs.print(errorMessage); //This gives med undefined

Any clue what I'm doing wrong or where I can improve my script in order to work?

Please let me know if further information is needed.


Best regards,
Adrian 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

var errorMessage = parser[0].error;

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

update as this

var errorMessage = parser[0].error;

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Awesome, thank you so much @Ankur Bawiskar !

Glad to help.

Please mark response helpful as well.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader