You need to sign in to do that
Don't have an account?
Pass parameter (string) to controller
I am trying to pass the value of a string that is held in a variable inside a javascript function on the visualforce page to the controller.
All that ever gets passed is null. Hoping someone can help me?
(I am trying to implement an external rich text editor b/c our customer needs the font/color/size options which are not available in native salesforce wysiwyg editor).
Here is the code:
<apex:pageBlock mode="edit"> <apex:pageBlockButtons > <apex:commandButton value="Save" title="Save changes" onClick="saveChanges()"/> <apex:commandButton action="{!cancel}" value="Cancel" title="Cancel changes"/> <apex:commandButton action="{!reset}" value="Reset to Default Text" title="Reset to default text"/> <apex:actionStatus startText="(Saving...)" stopText=""/> </apex:pageBlockButtons> <apex:outputfield value="{!Letter__c.Letter_Body_Default__c}" rendered="false"/> <apex:outputfield value="{!Letter__c.Letter_UID__c}" rendered="false"/> <apex:inputtextarea rows="60" cols="150" rendered="true" richtext="false" id="LetterTxtArea" value="{!Letter__c.Letter_Body__c}"/> <script type="text/javascript"> var myTextBox; var TextAreaID = document.getElementById("{!$Component.LetterTxtArea}"); var toolbar_1 = ["separator","undo","redo","separator","bold","italic","underline","separator","left","right","center","justify"]; var toolbar_2 = ["separator","fontfamily","fontsize","fontcolor","separator","ol","ul","separator","indent","outdent","separator","hyperlink"]; var toolbar_3 = ["separator","undo","redo","separator","bold","italic","underline","separator","left","right","center","justify","separator","fontfamily","fontsize","fontcolor","separator","ol","ul","separator","indent","outdent","separator","hyperlink"]; $(document).ready(function(){ //alert('swapping now'); myTextBox = $(TextAreaID).htmlbox({ toolbars:[toolbar_3], idir:"{!URLFOR($Resource.HTMLBox, 'images')}", about:false }); }); function saveChanges(){ var strHtml = myTextBox.get_html(); alert(strHtml); Save(strHtml); } </script> </apex:pageBlock>
The alert pops up with the value of the text box, including all of the html tags.
public class EndOfTermControllerExtension { private id letterID; private Letter__c letter; public String textBox{ get; set{ textBox = value; system.debug('value: '+value); } } Apexpages.StandardController controller; public EndOfTermControllerExtension(ApexPages.StandardController stdcontroller) { letter = (Letter__c)stdController.getRecord(); letterID = letter.id; controller = stdController; } public void doReset(){ letter.letter_body__c = letter.letter_body_default__c; } public PageReference saveletter() { // overwrite standard Save method //string text = string.valueof(lettertext); //String lettertext = ApexPages.CurrentPage().getParameters().get('textBox'); System.debug('textBox is '+textBox); Letter__c l = [SELECT reviewed__c, letter_uid__c, status__c, attention__c, letter_body__c FROM letter__c WHERE id = :letterID]; l.status__c = 'Reviewed'; l.attention__c = false; l.reviewed__c = true; l.letter_body__c = textBox; update l; Pagereference s = new PageReference('/apex/EOTL_CloseMePage'); // redirect to closer page return s; //return null; } public PageReference cancel() { // overwrite standard Cancel method controller.cancel(); //invoke standard Cancel method Pagereference c = new PageReference('/apex/EOTL_CloseMePage'); // redirect to closer page return c; } public PageReference reset() { doReset(); return Apexpages.currentPage(); //refresh current page }
All that ever gets passed to the controller is null.
Can someone help? I feel like I am SO close!!!
Thanks!!!
Hi - a coworker helped me with this. I can't believe how long this took me to figure out.
I had to use a hidden field and then I assigned the value of the string to the hidden field and passed it to controller.
Thanks!
All Answers
Can you post the Save code that the javascript calls?
Hi - it calls the actionfunction.
<apex:actionFunction name="Save" action="{!saveletter}">
<apex:param name="textBox" value="" assignTo="textBox"/>
</apex:actionFunction>
Sorry - I here it is updated:
i think you may need to add a rerender attribute to actually get the parameter passed to the controller - I've seen this before a number of times. I suspect the lack of a rerender affects the Ajax-ness of the call.
If there's nothing to sensibly rerender, I usually add an outputpanel around the whole page and rerender that, effectively refreshing the page.
Hi, thank you for your reply. I still cannot get it to work.
I added a rerender on the actionfunction. I also tried adding it to the command button to no avail.
Should I be using the assignTo attribute in the actionFunction? Or should I use the getParameters in the Page Reference? I am thinking (hoping) that is where the problem is.
I can't believe this is so hard to accomplish. Is it because of the html tags?
Here is all of the code.
and controller:
The VF Page always passes NULL to the controller for the variable.
The rerender needs to be on the actionfunction, as that is actually carrying out the form submission. I always use assignTo rather than inspecting the URL - it seems better to let the platform handle the data to me.
Here's an example actionfunction from my dev org:
This is invoked by the following commandbutton:
and the controller has:
Hi - a coworker helped me with this. I can't believe how long this took me to figure out.
I had to use a hidden field and then I assigned the value of the string to the hidden field and passed it to controller.
Thanks!