0

I am developing an Apex class which is making a webservice callout by supplying Account number as the request payload. If the Account is not found in the external system, the webservice is returning a status code of 200 with empty body. When I print the response body with the code below, it is printing blank.

System.debug('API Response Body = ' + hrResponse.getBody());

The output statement in the debug log is below.

API Response Body = []

So, in order to skip processing this webservice response, I am checking the response body is blank or null or empty with a method Util.IsNullOrEmpty. The implementation of the method is below.

public void IsNullOrEmpty(String str) {
    if (str == null || str == '' || String.isBlank(str) || String.isEmpty(str) {
        return true;
    }
}

So, in my original apex class, I am checking the response body with the code below.

if (Util.IsNullOrEmpty(hrResponse.getBody()) {
    System.debug('Webservice response is blank. Skipping this Account');
    continue;
}

Though the webservice response is blank, the above IF condition is not satisfied and the record is not skipped. Instead, it skips the IF statement and continues to process the empty message body.

Does anyone know why this is not working?

1
  • How is [] treated as blank? You need not check so many checks for null checks . You can just check for isBlank(inputString). – Nagendra Singh Nov 20 '20 at 5:19
0

I found the issue. The actual response body is [], not blank or null. So, I had to change my if condition as below.

if (Util.IsNullOrEmpty(hrResponse.getBody() || hrResponse.getBody() == '[]') {
    System.debug('Webservice response is blank. Skipping this Account');
    continue;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.