+ Start a Discussion
davecrusodavecruso 

How to pass a variable from an VF page into an Apex controller, if unrelated to object?

Hi there, 

 

I need to pass a variable from my VF page into an Apex controller, but am having trouble. Here's my use case:

 

A user fills out a form. She/he selects a box called "e-mail colleagues". If checked, the controller runs through a list of his/her colleagues and sends them an e-mail. If unchecked, that part of the controller is skipped when the form is posted.

 

So, there are two things I'm trying to accomplish:

 

(1) Get a checkbox into a page.

(2) Add a line to the controller that checks to see if the checkbox value of "1" or "yes" was posted, and if so, do some code. 

 

So far, I've been following the Apex:selectRadio tutorial, but this seems overkill for what I need to do. Are there any easier ways, and if so, might someone please post a sample snippet?

 

Thanks SO MUCH!

--Dave 

 

 

davecrusodavecruso

It seems that one option might be something like including code like this:

 

<input type="radio" name="sendemail" value="1" />

 

and then, on the controller-side, something like:

 

if (pageRef.getParameters().get('sendemail') = '1') { // some code}

 

Is there some way that this could work?

 

Thanks again!

--Dave 


 

bob_buzzardbob_buzzard

A better way would be to use an apex component that is backed by a property from your controller.  Then your controller property will be updated automatically with the user's selection.

 

Here's an example using an inputtext - hopefully you get the idea:

 

Page:

 

 

<apex:inputText id="start_cal" size="10" value="{!startDate}" />

 Controller:

 

 

public String startDate {get; set;}

 

 Then you can use the start date property in the rest of your controller code quite happily.

 

The VisualForce developer's guide has lots of examples of this.