-2

I have a method:

public static String doCallout(String requestJson, String recordId){
    HttpRequest req = new HttpRequest();
    req.setEndpoint(END_POINT_URL);
    req.setHeader('Content-Type', 'application/json');

    req.setMethod(HTTP_METHOD);
    req.setBody(requestJson);
    Http http = new Http();
    HTTPResponse res = http.send(req);        
    if(res.getStatusCode() == 202){
        ResponseWrapper response = (ResponseWrapper)JSON.deserializeStrict(res.getBody(),ResponseWrapper.class);
        Service_Plan__c servicePlanToUpdate = [SELECT id, Most_Recent_PDF_Link__c FROM Service_Plan__c WHERE Id=: recordId];
        servicePlanToUpdate.Most_Recent_PDF_Link__c = response.location;
        update servicePlanToUpdate;
        return 'URL Generated Successfully. ';
    }else if(3 > 5){
        String errorMessage = 'Error Occurred with status code: '+res.getStatusCode()+' And Status: '+res.getStatus();
        return errorMessage+'. Error Message: '+res.getBody();
    }
   }

im getting an error on the else-if statement... ?I dont know why since I am returning a string.... the 3>5 condition is just for testing... ive tried everything but the developr console just does not like it... am i missing something?

2 Answers 2

6

You're missing the return statement for when you don't satisfy the if condition and also don't satisfy the else if condition.

The code inside your else if is unreachable (because 3 > 5 will always evaluate to false), so in that situation (where you don't satisfy the if condition) your code will end up in a path with no return statement.

If you have any possible code path in a method with a non-null return type that doesn't contain a return, that's an issue.

It goes a bit further than that, though. The compiler doesn't seem to have a mechanism to check if the conditions for an if-else if chain are comprehensive1. If the last statement (maybe expression is the word I'm looking for?) in your method is an if-else if, then Salesforce simply requires you to specify the else.

To illustrate, the following anonymous apex will not compile, even though if(true) is comprehensive

public class MyClass {
    public Integer doAThing(){
        if(true){
            return 1;
        }
    }
}

but if we include the else, then it will compile

public class MyClass {
    public Integer doAThing(){
        if(true){
            return 1;
        } else {
            // This else (and the return) is required by the compiler
            // ...even if this block is unreachable
            return 0;
        }
    }
}

alternatively, if the final statement is a return, then you don't need to have the else

public class MyClass {
    public Integer doAThing(){
        if(true){
            return 1;
        }

        // This is fine too
        // ...and also unreachable
        return 0;
    }
}

1: Though in Salesforce's defense, trying to decide if an if-else if chain covers all possible conditions sounds like it would be a major pain to try to do. It might even be an NP-complete class problem (as it strikes me as being similar to 3SAT).

0

I solved this issue by creating a String variable in the method and then modifying the value of that string variable based on the condition.

1
  • 2
    This isn't really an answer that isn't covered by Derek's answer. This answer also won't really help anyone else that has this problem. Either update this answer with the actual code so it helps someone, or better yet, delete this answer and mark Derek's as the accepted answer.
    – Nick C
    Commented Sep 12, 2023 at 3:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .