+ Start a Discussion
SoozeeSoozee 

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!!!

Best Answer chosen by Admin (Salesforce Developers) 
SoozeeSoozee

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

bob_buzzardbob_buzzard

Can you post the Save code that the javascript calls? 

SoozeeSoozee

Hi - it calls the actionfunction.

<apex:actionFunction name="Save" action="{!saveletter}">
<apex:param name="textBox" value="" assignTo="textBox"/>
</apex:actionFunction>

SoozeeSoozee

Sorry - I here it is updated:

 

<apex:actionFunction name="Save" action="{!saveletter}">
<apex:param name="textBox" value="" assignTo="{!textBox}"/>
</apex:actionFunction>

 


bob_buzzardbob_buzzard

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.

SoozeeSoozee

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.

 

<apex:page StandardController="Letter__c" extensions="EndOfTermControllerExtension" standardStylesheets="true" showHeader="false">  

 
<apex:includeScript value="{!URLFOR($Resource.jquery, 'js/jquery-1.7.2.min.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.HTMLBox, 'htmlbox.min.js')}"/>

 
<apex:form id="letterbodyForm" >
<apex:outputPanel id="thePanel">
<apex:actionFunction name="passTheString" action="{!saveletter}" >
	<!-- <apex:param name="myTextBox" value="" assignTo="{!myTextBox}"/>-->
	<!-- <apex:param name="myTextBox" value="" assignTo="{!theHTML}"/> -->
	<apex:param name="myTextBox" value=""/>
</apex:actionFunction>
    <br/><font face="verdana" size = "3">&nbsp;&nbsp;Letter Text for <b>{!Letter__c.Account__r.Name}</b> - Edited By {!$User.FirstName} {!$User.LastName}</font><br/><br/>
    <font face = "verdana" size ="1">&nbsp;&nbsp;Edit the text below and click Save, Cancel or Reset</font><br/><br/>


        
        
        <apex:pageBlock mode="edit">
        
            <apex:pageBlockButtons >
                <!-- <apex:commandButton onClick="update(andRefresh())" value="Save" title="Save text changes"/> -->
              <!--  <apex:commandButton action="{!save}" value="Save" title="Save text changes"/> -->
              <!-- <apex:commandButton action="{!save}" value="Save" title="Save changes" onClick="beforeTextSave()"/> --> 
              <!-- <apex:commandButton value="Save" title="Save changes" onClick="passTheString(userInput())" /> -->  
              
              <apex:commandButton value="Save" title="Save changes" onClick="saveChanges(); return false;" reRender="thePanel"/>  
                       
                
                <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:inputHidden value="{!Letter__c.Status__c}" id="hiddenStatus"/>
             <apex:outputfield value="{!Letter__c.Status__c}" /> -->
             
              
            
            
			           
            <apex:inputtextarea rows="60" cols="150" rendered="true" richtext="false" id="LetterTxtArea" value="{!Letter__c.Letter_Body__c}"/>
            

			<!-- <apex:inputHidden value="{!myHTML}" id="theField"/> -->
  		      
 		


            
            <script type="text/javascript">
            //var myHTMLString = document.getElementById("{!$Component.inptHdn}");
            
            var strHtml;
            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 userInput(){
            //	var UInput=document.getElementById("{!$Component.LetterTxtArea}").value;
            //	alert(UInput);
            //	return UInput;
        	//}
        
			
			
			
			function saveChanges(){
				alert('saving changes now');
				strHtml = myTextBox.get_html();
				//var myHTMLString = document.getElementById("{!$Component.inptHdn}");
				//var UInput=document.getElementById("{!$Component.LetterTxtArea}").value;
				//var UInput=document.getElementById("{!$Component.LetterTxtArea}").get_html();
				//alert('just assigned letter text to variable');
				//alert(UInput);
				//String str = 'my test value';
    			//strHtml = UInput.get_html();
    			//alert('just got html');
    			//document.getElementById(myHTMLString).value = strHtml;
    			
				alert(strHtml);
				passTheString(strHtml);
				//passTheString(UInput);
				//saveletter();
			}
			</script>
		
		</apex:pageBlock>
 		</apex:outputPanel>
    	</apex:form>


            

    
 
</apex:page>

 

and controller:

 

public class EndOfTermControllerExtension {
   
    private id letterID;
    private Letter__c letter;
    //public String myTextBox{get;set;}
    //public String myHTML{get;set;}
    public String theHTML { get; set; }
    
    
    public void setmyHTML(String myHTML)
    {
        system.debug('myhtml '+myHTML);
        //this.userinput = userinput;
    }   
     
    
    Apexpages.StandardController controller;
    public EndOfTermControllerExtension(ApexPages.StandardController stdcontroller) {
        letter = (Letter__c)stdController.getRecord();
        letterID = letter.id;
        //accountId = stdController.getId();
        controller = stdController;
        //advID = UserInfo.getUserId(); // gets ID of Advisor logged in
        //advID = '00530000003tA7cAAE';
        system.debug('thehtml inside main controller'+thehtml);
  }

		
 
     
    public void doReset(){
        letter.letter_body__c = letter.letter_body_default__c;
    }   
    
    /*
    public void updateStatus(){
    	//string text = string.valueof(lettertext);
    	String lettertext = ApexPages.CurrentPage().getParameters().get('textbox');  
    	System.debug('letter text is '+lettertext);
    	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 = :lettertext;
		update l;
    }
    */

   public PageReference saveletter() { // overwrite standard Save method
        //String interviewdate=Apexpages.currentPage().getParameters().get('interviewDate'); 
        //String htmlstring=Apexpages.currentPage().getParameters().get('htmlstring'); 
        //String htmlstring = Apexpages.currentPage().getParameters().get('myHTML'); 
        
        String myHTML = Apexpages.currentPage().getParameters().get('myTextBox'); 
        //string text = string.valueof(lettertext);
      	//String lettertext = ApexPages.CurrentPage().getParameters().get('textBox');  
    	System.debug('inside saveletter theHTML is '+myHTML);
    	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 = myTextBox;
		l.letter_body__c = myHTML;
		update l;
      //updateStatus();
      //controller.save(); //invoke standard Save method
      
      System.debug('call close me');
      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      
  }


     
}

 

The VF Page always passes NULL to the controller for the variable.

bob_buzzardbob_buzzard

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:

 

<apex:actionFunction name="deleteContact" action="{!deleteContact}" rerender="contactList" status="working">
    <apex:param name="contactIdent" value="" assignTo="{!chosenContactId}"/>
</apex:actionFunction>

 This is invoked by the following commandbutton:

 

<apex:commandButton id="confirmDelBtn" value="Delete" rerender="contactList" onclick="hidepopup('deletecontent'); alert('Deleting contact ' + idToDelete); deleteContact(idToDelete); return false;" status="working"/>

 and the controller has:

 

    // the chosen contact id - used when deleting a contact
    public Id chosenContactId {get; set;}
    
...


    public void deleteContact()
    {
       if (updateContacts())
       {
          if (null!=chosenContactId)
          {
               Contact cont=new Contact(Id=chosenContactId);
              delete cont;
       
              contacts=null;
              chosenContactId=null;
          }
       }
    }
    

 

SoozeeSoozee

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!

This was selected as the best answer