0

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;
      congratsPage.setRedirect(true);
      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[] {'[email protected]'}; 



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


        mail.setWhatId(WhatId);

         mail.setToAddresses(toAddresses);



        mail.saveAsActivity = false;
        mail.setTargetObjectId(UserId);
     mail.setReplyTo('[email protected]');
       mail.setSenderDisplayName('Proteus Leadership');

       mail.setBccSender(false);
       mail.setUseSignature(false); 

       mail.setTemplateId('00X90000001FPod');


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

       PageReference thanksPage = Page.thankyou;
      thanksPage.setRedirect(true);
      return thanksPage;

         } 


}

thank you for any help.

5
  • 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, 2014 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? Aug 25, 2014 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, 2014 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. Aug 26, 2014 at 1:13
  • what is the actual name of {!RETURNEDSTRING}
    – user28212
    Jan 27, 2016 at 20:20

1 Answer 1

3
  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....

4
  • 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(){ Aug 25, 2014 at 23:49
  • just change the void to string
    – Eric
    Aug 26, 2014 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;" /> Aug 26, 2014 at 0:29
  • Hi Eric, It worked without changing the VF page. Thank you for your help! Aug 26, 2014 at 0:44

You must log in to answer this question.

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