I have a VF page with a submit button, when the user clicks on the submit button it sends an email. I need the page to redirect to a thank you page after they hit submit.

I am getting this error:

Save error: Void method must not return a value

But I don't know how to redirect without returning a value. Here is the controller APEX code.

    global class proteusProposalCon {
    global ApexPages.StandardController controller;
    global proteusProposalCon(ApexPages.StandardController controller) {
        this.controller = controller;

      global PageReference saveAndCongrat() {
      controller.save(); // This takes care of the details for you.
      PageReference congratsPage = Page.thankyou;
      return congratsPage;

    global List<Opportunity> listOfOpportunity {get; set;}
    global Opportunity OpportunityObj {get;set;}

    //global Object get(String Proposals_Senders_Email__c);

    Webservice static void sendEmailFunction(){

       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

      String[] toAddresses = new String[] {'joe.stuart@proteusleadership.com'}; 

         Id WhatId = ApexPages.currentPage().getParameters().get('id');
        Id UserId = ApexPages.currentPage().getParameters().get('userId');



        mail.saveAsActivity = false;
       mail.setSenderDisplayName('Proteus Leadership');



       if (!Test.IsRunningTest()) Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

       PageReference thanksPage = Page.thankyou;
      return thanksPage;



thank you for any help.

  • 1) You cannot return anything from a void method 2) why do you have your sendEmailFunction as webservice method ( I am not too sure you cannot have the return type as anything other than void for webservice method) – Rao Aug 25 '14 at 23:15
  • can I change it from a webservice method? I am new to APEX and from the examples I thought it needed to be a webservice. How would I write it if it wasn't a webservice? – proteuscanvas Aug 25 '14 at 23:43
  • If you are using VF, there is no obvious reason why the server side should be a webservice - a normal VF controller would do. Webservice would be more relevant if the client side didn't use VF at all. – cropredy Aug 26 '14 at 0:49
  • I see, so it is more like a SOAP API... I have removed it and set it to string and I have it working now. Thanks. – proteuscanvas Aug 26 '14 at 1:13
  • what is the actual name of {!RETURNEDSTRING} – user28212 Jan 27 '16 at 20:20
  1. In your class set the return type to String
  2. change the return to return string.valueOf(thanksPage.getURL());

Then on the VF page

use parent.location.href = {!RETURNEDSTRING}

to navigate to the returned URL. Keep in mind to check for null or errors....

  • Hi Eric, thank you for your answer, I am very new to APEX so please excuse me. How do I set set the return type to a String? This is what I have - global string sendEmailFunction(){ – proteuscanvas Aug 25 '14 at 23:49
  • just change the void to string – Eric Aug 26 '14 at 0:08
  • Thanks Eric, that was all good now with the VF Page I am currently using a apex:commandButton, do I add the href code into the action attribute? My button looks like this: <apex:commandButton action="{!sendEmailFunction}" value="Submit" id="theButton" style="background:#00c9ff; color:#ffffff; font-size:18px; font-weight:bold; padding:6px; display:block; width:250px; float:left; margin-top:6px;" /> – proteuscanvas Aug 26 '14 at 0:29
  • Hi Eric, It worked without changing the VF page. Thank you for your help! – proteuscanvas Aug 26 '14 at 0:44

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.