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
Michael Webb 15Michael Webb 15 

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

I am trying to just add a sucess message when the user enters a case, I am obviously new to Apex and can't figure out how to do this. When I put this in I get the following error message on the success - ​Keep getting the error message - Non-void method might not return a value or might have statement after a return statement.

 // Insert the case
                INSERT c;
                if (objAttachment.Body != null) {
                    // Insert attachment if exists
                      objAttachment.ParentId = c.Id;
                    insert objAttachment;
                }
                
                 ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM, 'Success!'));
            } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
        }
    }

Thank you very much for any information on how to do this. 
Aparna Bochare 6Aparna Bochare 6
Hi,

Make this method as void. You can refer below sample code:

Apex Controller:

public class createCase{

    public createCase(){}
    
    public void insertCase(){
          try{
                Account acc = new Account();
                acc.Name = 'Test';
                insert acc;
                
                Case c1 = new Case (subject='testcase asia new account',AccountId=acc.id,Origin = 'Email');
                
                INSERT c1;
                
                
                 ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM, 'Success!'));
            } catch (Exception e) {
                ApexPages.addMessages(e);
                
            }
        
    }

}

VF Page: 

<apex:page controller="createCase">
  <apex:form>
 <apex:pageMessages escape="false" id="messages"/>
  <apex:commandButton action="{!insertCase}" value="click" reRender="messages"/>
  </apex:form>
</apex:page>


Thanks,
Aparna
VamsiVamsi
Please verify the method return type and make sure you use the pagereference as return type for method. Because you have returned null at the end 

or else you use void as return type for that method and remove the "return null" at the end.
Michael Webb 15Michael Webb 15
Still no sure what I am doing wrong.. Hmm tried a dozen different incarnations. Thank you! Here is the whole code. I wanted to replace the return with the success message that was posted earlier Thank you in advance!

public class SubmitCaseController {     
    public Case c { get; set; }
    // public String acctNum { get; set; }
    public SelectOption[] caseTypes { get; set;}
    
    public List<Case> UserCaseList
    {
        get
        {
            return [Select CreatedDate, Status, Origin, ClosedDate, CaseNumber, Subject, Owner.alias
                    From Case
                    Where Contact.Email = :UserInfo.getUserEmail() AND Origin = 'SF' AND Status NOT IN ('Closed','Completed') ORDER BY CaseNumber DESC limit 10]; 
                
        }
    }
    public Attachment objAttachment { get; set; }
    public SubmitCaseController(ApexPages.StandardController controller) {
        c = new Case();
        this.caseTypes = new SelectOption[]{};
        this.caseTypes.add(new SelectOption('-', '-'));
        this.caseTypes.add(new SelectOption('Question','Question'));
        this.caseTypes.add(new SelectOption('Problem', 'Problem'));
        this.caseTypes.add(new SelectOption('Enhancement', 'Enhancement'));
        objAttachment = new Attachment();
   }
    
    public Contact userContact
    {
        get
        {
            
            Contact cnt = [SELECT Id FROM Contact WHERE Email = :c.SuppliedEmail LIMIT 1];
            if (cnt != null) return cnt;
            else return null;
        }
    }

    public PageReference submitCase() {
       // List<Account> accts = [SELECT Id FROM Account WHERE AccountNumber = :acctNum Limit 100];

        // if (accts.size() != 1) {
            //ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Invalid account number');
            //ApexPages.addMessage(msg);
            //return null;
      //  } else {
            try {
                if (c.Type == '-') 
                {
                     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please Select a Type'));
                     return null;
                }

                // c.AccountId = accts.get(0).Id;  
                c.SuppliedEmail = UserInfo.getUserEmail();
                c.Origin = 'SF';
                //c.RecordTypeId = '012W00000000Xmb';
                c.RecordTypeId = '012000000004yTO';
                c.Product__c = 'Salesforce.com';
                c.OwnerId = '00500000007HkiF';
                c.No_Closed_Email__c = FALSE;
                if (c.Customer_Facing__c == TRUE) {
                c.Priority = 'High -2';
                } else {(c.Customer_Facing__c = FALSE);
                
                (c.Priority = 'Med - 3');
                 }
                
                // now look for an associated contact with the same email
                Contact cnt = userContact;
                if (cnt != null) c.ContactId = cnt.Id;
                
                // Specify DML options to ensure the assignment rules are executed
                // Database.DMLOptions dmlOpts = new Database.DMLOptions();
                // dmlOpts.assignmentRuleHeader.useDefaultRule = true;
                // c.setOptions(dmlOpts); 
                
                // Insert the case
                INSERT c;
                if (objAttachment.Body != null) {
                    // Insert attachment if exists
                      objAttachment.ParentId = c.Id;
                    insert objAttachment;
                }
                
                return new PageReference('https://na31.salesforce.com/apex/submitcasethankyou');
            } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
        }
    }
Balasubramaniam 07Balasubramaniam 07
Hi,

Try this,

public PageReference submitCase() {
       // List<Account> accts = [SELECT Id FROM Account WHERE AccountNumber = :acctNum Limit 100];

        // if (accts.size() != 1) {
            //ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.FATAL, 'Invalid account number');
            //ApexPages.addMessage(msg);
            //return null;
      //  } else {
            try {
                if (c.Type == '-') 
                {
                     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please Select a Type'));
                     return null;
                }else {
                
                    c.SuppliedEmail = UserInfo.getUserEmail();
                    c.Origin = 'SF';              
                    c.RecordTypeId = '012000000004yTO';
                    c.Product__c = 'Salesforce.com';
                    c.OwnerId = '00500000007HkiF';
                    c.No_Closed_Email__c = FALSE;
                    if (c.Customer_Facing__c == TRUE) {
                    c.Priority = 'High -2';
                    } else {(c.Customer_Facing__c = FALSE);                
                    c.Priority = 'Med - 3';
                    }
                    
                    // now look for an associated contact with the same email
                    Contact cnt = userContact;
                    if (cnt != null) 
                        c.ContactId = cnt.Id; 
                    
                    INSERT c;
                    if (objAttachment.Body != null) {
                        // Insert attachment if exists
                          objAttachment.ParentId = c.Id;
                        insert objAttachment;
                    }
                    
                    return new PageReference('https://na31.salesforce.com/apex/submitcasethankyou');
                }
            } catch (Exception e) {
                ApexPages.addMessages(e);
                return null;
            }
        }