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
nduchnduch 

How to get user input from Visualforce to Apex

Hi,

I have a text field in Visualforce that display email template automatically to the user.

 

<apex:inputTextarea  value="{!templateBody}" />

 

The user then edits the text and clicks on a button. What I want to do is  - when the user clicks on the button I want to get the user input to Apex. How can I do that?

 

I suppose I have to use setter method for templateBody Apex variable, but I dont know how to reference the inputTextarea field.

 

Many thanks for help!

Best Answer chosen by Admin (Salesforce Developers) 
nduchnduch

HI John,

since my controller is an extension  of the standard controller, I have overriden the save() method with new functionality AND the previous functionality stayed, i.e. It is saving the user input PLUS doing what I have in my save() method.

 

Hard to understand for me at first, sorry!

 

Many thanks for the help!:smileyhappy:

All Answers

Chris JohnChris John

Hi, you are on the right track.

 

You just need a setter method in the page controller.

 

There is an example in the VF docs here:

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_controller_methods.htm

 

Hope that helps,

 

-cj

nduchnduch

HI John,

I have a set method for this variable, but I dont know how can I invoke it in the button's code:

 

This is my setter:

 

    public void setTemplateBody(String s) {
        templateBody= s;
    }

 

I need something like this in the button's code:

 

this.setTemplateBody(user input from inputTextArea field);

 

But I dont know how to get the String argument for the setter method.

nduchnduch

Or do I have to call the controller.getRecord() method to get the user input?

Chris JohnChris John

Check out the 'Action Methods' section of that doc link.

 

You can specify a controller method to invoke on clicking the button, e.g. to call a 'save' method:

 

<apex:commandButton action="{!save}" value="Save" id="theButton"/>
nduchnduch

I need this button to do more than just save. Therefore I cannot use the controller's save method.

 

I want the button to save + create an email from the user input + send the email.

 

Thank you for your understanding.

Chris JohnChris John

The example in the docs was to call a save method, but you can define whatever logic you need in a controller method and call that as the action on the button click.

 

Hope that helps.

nduchnduch

But how to write a controller method that would save user input + do more actions?

 

 

nduchnduch

HI John,

since my controller is an extension  of the standard controller, I have overriden the save() method with new functionality AND the previous functionality stayed, i.e. It is saving the user input PLUS doing what I have in my save() method.

 

Hard to understand for me at first, sorry!

 

Many thanks for the help!:smileyhappy:

This was selected as the best answer