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
mandycmandyc 

Non-void method might not return a value or might have statement after a return statement

Hello,

 

Can someone help me refactor the below code? If I take out the line return '??'; below then I get the following error:

 

Compile Error: Non-void method might not return a value or might have statement after a return statement. at line

 

I don't understand why I need to return something at line 73 (where I have return '??')

 

Thanks in advance!

 

 

    @HttpPatch
    global static String doPatch() {
    
    RestRequest req = RestContext.request;
    
    // see if a suborder number was part of the uri
    String suborder_no = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
    
    if (suborder_no != ''){
        Savepoint sp = Database.setSavePoint();
        Order__c[] suborder = [SELECT Name FROM Order__c WHERE Name =: suborder_no];
        if(suborder.size() > 0){
            List<Opportunity> oppToUpdate = new List<Opportunity>();
            for(Order__c ordr : suborder){
                Id oppId = ordr.Opportunity_Name__c;
                oppToUpdate = [SELECT Id FROM Opportunity WHERE Id =: oppId AND StageName <> 'Closed / Lost'];
                if(oppToUpdate.size() > 0){
                    for(Opportunity o : oppToUpdate){
                        o.StageName = 'Closed / Lost';
                    }
                    update oppToUpdate;
                    return 'success';
                }
                else{
                    return 'Error: opportunity already closed';
                }
            }
            return '??';
        }
        else{
            return 'Error: suborder does not exist';
        }
    }
    else{
        return 'Error: no suborder was passed to service';
    } 
}  

 

bob_buzzardbob_buzzard
If there are no entries in the suborder list, the code will not enter the body of the 'for (Order__c ordr : suborder)' loop and would not encounter a return statement, as the rest are in else statements.
mandycmandyc

Doesn't this line ensure that there is something in the suborder list?

 

if(suborder.size() > 0){
bob_buzzardbob_buzzard

You are correct.  In that case I don't know. Maybe the compiler is as good as I am at spotting those checks :)