You need to sign in to do that
Don't have an account?
Pseudodarwinist
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.
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.
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
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;
}
Can you explain ((reason != '')?',':'') for me?