function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PseudodarwinistPseudodarwinist 

HTTPRESPONSE.getBody(): getting all error messages from response and putting it in a field

In one of the Integrations we are receiving the response and from the response, we are putting the errors in a field called "Observation".
below is the response that we receive when there is error.
          "errors": [
            {
              "code": "PAC_UTILS_E0005",
              "reason": "Error in Validation of data.Please look into the error.",
              "uuid": null,
              "timeStamp": "2018-08-02 08:11"
            },
            {
              "code": "PAC_UTILS_E0005",
              "reason": "property:documentType|error:Cannot be null",
              "uuid": null,
              "timeStamp": "2018-08-02 08:11"
            }
          ]
We only parse "reason" and put it in the " Observation" field for the user to correct the missing field.We use below Code to fetch the error response:
String reason = res.getBody().subStringBetween('reason":"','",');
                    WpRes respFail = new WpRes();
                    respFail.result = reason;
But my issue is i want to put both "reason" that we have received in the response .The above code res.getBody().subStringBetween('reason":"','",') only gets the first reason "Error in Validation of data.Please look into the error." But i want both reason in my observation fieeld so that it is easier for users to correct the errors .
My expected result should look like "Error in Validation of data.Please look into the error.property:documentType|error:Cannot be null"

Any immidiate help is appreciated.
 
Best Answer chosen by Pseudodarwinist
Payal Popat 27Payal Popat 27
Try this:

Httpresponse res = new Httpresponse();
res.setBody('{"errors": [{"code": "PAC_UTILS_E0005","reason": "Error in Validation of data.Please look into the error.","uuid": null,"timeStamp": "2018-08-02 08:11"},{"code": "PAC_UTILS_E0005","reason": "property:documentType|error:Cannot be null","uuid": null,"timeStamp": "2018-08-02 08:11"}]}');

wrap errorlst = (wrap) JSON.deserialize(res.getBody(), wrap.class);

string reason = '';
for(errors err:errorlst.errors){
  reason = reason + ((reason != '')?',':'')+ err.reason;
}
public class wrap{
  List<errors> errors;
}
public class errors{
  string code;
  string reason;
}

All Answers

Payal Popat 27Payal Popat 27
Try this:

Httpresponse res = new Httpresponse();
res.setBody('{"errors": [{"code": "PAC_UTILS_E0005","reason": "Error in Validation of data.Please look into the error.","uuid": null,"timeStamp": "2018-08-02 08:11"},{"code": "PAC_UTILS_E0005","reason": "property:documentType|error:Cannot be null","uuid": null,"timeStamp": "2018-08-02 08:11"}]}');

wrap errorlst = (wrap) JSON.deserialize(res.getBody(), wrap.class);

string reason = '';
for(errors err:errorlst.errors){
  reason = reason + ((reason != '')?',':'')+ err.reason;
}
public class wrap{
  List<errors> errors;
}
public class errors{
  string code;
  string reason;
}
This was selected as the best answer
PseudodarwinistPseudodarwinist
Thanks Payal.It works now.
Can you explain ((reason != '')?',':'') for me?