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
tunedogtunedog 

Writing values to hidden fields

I have a set of VF forms that I have made publicly accessible so as to capture data on a custom object.

I need to be able to hardcode a value in to a hidden field that can be passed onto my object.

 

I know this is easily achieved with Web to Lead (Eg: <input type=hidden value=webform id=LeadSource>)

but how do I achieve the same thing using VF? I can't even work out how to get this done using name/value pairs in the URL to write to the field.

 

Any help would be appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

Have you seen the apex:inputHidden component?  It allows you to bind a hidden input to an sobject field and then you can update the value using javascript.  Something like this:

 

<apex:page standardController="account" id="thePage">
  <apex:form id="theForm">
    <apex:inputField value="{!account.name}"/>
    <apex:inputHidden value="{!account.description}" id="hiddenField"/>
    <apex:commandButton value="save" action="{!save}"/>
      <script>
        document.getElementById('{!$Component.theForm}').elements['{!$Component.hiddenField}'].value = 'this is a description';
      </script>
  </apex:form>
</apex:page>

 

If you enter an account name and hit save (this is assuming in your own org you don't have any other required fields on account), then your account record will be created and 'this is a description' will wind up in your description field.

 

All Answers

SteveBowerSteveBower

Perhaps a different take on the problem might help:

 

If you have a VF page, you're either using the standard controller, or a custom controller / extension.   If it's the former, and you're always going to use the same hardcoded value, can you just set a default on the field in the field definition?

 

If it's the latter, and you have Apex code, can you just hardcode the value there?  

 

It's unclear to me why you want to hardcode something in the VF page and then pass it to the controller instead of just putting it in the controller directly?

 

 

Having said that... if you put

 

<input type="hidden" name="peanuts" value="popcorn" />

 

into your VF page inside the <apex:form> structure, and use:

 

String peanuts = ApexPages.currentPage().getParameters().get('peanuts');

 

Inside an Action in your controller,  (because that hidden field is only being added to the URL when an Action is requested by the page), then it should pick up the value from the URL parameters.

 

Best, Steve.

tunedogtunedog

Thanks Steve,

 

The hidden field value is not a constant (there is more than one form writing to the custom object and each will have a different hidden value), so a default value on the sObject field isn't going to do the trick.

 

Up until this point I had managed to complete the entire VF page without having to create any controller or extensions.... I was hoping I wouldn't have to.

I guess this means I have to write and extension for the Save action then huh?!?

 

SteveBowerSteveBower

Probably.  

 

I don't know the nature of the dynamic behavior you are trying to capture.  Since the field in question isn't constant it must be calculated by your code (I presume in Javascript) in order to get the value you want to save.   One possibility would be to save the precursor value(s) to your object in some extra fields that you create for this purpose, and then use a formula field (or field update) to set the actual value for the object.

 

EG. if you want to store the area of a rectangle into your object, and the users are entering the width and height, you could calculate the area on your page and try to save it, or save the width and height and calculate the area via formula.

 

Of course this breaks down pretty quickly if your dynamic calculatation is anything complex, but it's a possibility.  Otherwise, yes, I think it's code writing time. :-)

 

Best, Steve.

tunedogtunedog

Thanks for your help on this Steve.

 

Basically a single form may be accessed from multiple domains (via an iframe).

Part of the requirement of these forms is that the source domain is captured on the sObject record.

 

I think at this stage I have established that I can include the domain in the form URL and use something like:

 

ApexPages.currentPage().getParameters().get('domain');


to capture the domain - I am just little stuck on how to then pass this to the sObject record on save.

Any pointers?

jwetzlerjwetzler

Have you seen the apex:inputHidden component?  It allows you to bind a hidden input to an sobject field and then you can update the value using javascript.  Something like this:

 

<apex:page standardController="account" id="thePage">
  <apex:form id="theForm">
    <apex:inputField value="{!account.name}"/>
    <apex:inputHidden value="{!account.description}" id="hiddenField"/>
    <apex:commandButton value="save" action="{!save}"/>
      <script>
        document.getElementById('{!$Component.theForm}').elements['{!$Component.hiddenField}'].value = 'this is a description';
      </script>
  </apex:form>
</apex:page>

 

If you enter an account name and hit save (this is assuming in your own org you don't have any other required fields on account), then your account record will be created and 'this is a description' will wind up in your description field.

 

This was selected as the best answer
tunedogtunedog

That's exactly what I need!!

I am going to test that out now and let you know how it goes.

 

Thanks!!!

tunedogtunedog

I have tested your example code and it works fine... 

but when I try modifying the field that it references, the hiddenInput ID and the value (as highlighted below) it stops working. NB: the form itself works, just not this hidden value piece.

 

Is there any reason this code might not work with a custom object/custom field?

Any ideas what I am doing wrong?

 

Code:

 

 

<apex:page showHeader="false" standardController="Enquiry__c" standardStylesheets="false" id="thePage">

<apex:form id="theForm">
<body style="margin:0">

<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse;">
    <tr>
        <td style="padding:10px" align="center">    
        	<img src="http://www.prmaustralia.com.au/MAB/twentyenterprise_Header.jpg" width="780" height="100" align="middle" /></td>
    </tr>

</table>

<table height="28" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse; width:800px;">
    <tr>
    	<td style="padding-left: 20px; padding-right:10px; width:510px;"><font face="Arial Unicode MS" style="font-size: 12pt"><b>Subscribe &amp; Register Your Details</b></font><font face="Myriad Pro" style="font-size: 9pt;"><br>
		</font><font face="Arial Unicode MS" style="font-size: 9pt">Please 
		complete the required fields <font color="#FF0000">*</font> to receive 
		exclusive project updates and offers.<br/></font><br />
    	
        <table width="480" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse;">
        
        	<tr>
            	<td width="100px" style="padding-left:10px"><font face="Arial Unicode MS" style="font-size: 9pt; text-align: left;"><span class="style2">First Name:</span><font color="#FF0000">*</font></font><span style="text-align: left"></span></td>
                <td>
                <apex:inputField id="FirstName" value="{!Enquiry__c.First_Name__c}" style="width:140px" />
                </td>
                
            	<td width="100px" style="padding-left:10px"><font face="Arial Unicode MS" style="font-size: 9pt; text-align: left;">Last Name:</span><font color="#FF0000">*</font></font></td>
                <td>
                <apex:inputField value="{!Enquiry__c.Last_Name__c}" style="width:140px" />
                </td>
            </tr>
        
        </table>
        
        <table width="480" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse;">
        	<tr>
            	<td width="100px" style="padding-left:10px">
                <font face="Arial Unicode MS" style="font-size: 9pt; text-align: left;">Email:<font color="#FF0000">*</font></font>
                </td>
                <td>
                <apex:inputField value="{!Enquiry__c.Email__c}" style="width:380px" />
                </td>
            </tr>
        	<tr>
            	<td width="100px" style="padding-left:10px">
                <font face="Arial Unicode MS" style="font-size: 9pt; text-align: left;">Phone:</font>
                </td>
                <td>
                <apex:inputField value="{!Enquiry__c.Phone__c}" style="width:280px" />
                </td>
            </tr>
        	<tr>
            	<td width="100px" valign="top" style="padding-left:10px">
                <font face="Arial Unicode MS" style="font-size: 9pt; text-align: left;">Enquiry:</font>
                </td>
                <td>
                <apex:inputTextarea value="{!Enquiry__c.Description__c}" style="width:280px" rows="4" />
              </td>
          </tr>              
        </table>
        
        <table width="480" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse;">         
          <tr>
          	<td align="center">
          		<img border="0" src="http://www.prmaustralia.com.au/MAB/v_line.gif" width="463" height="9" style="margin-top:5px;">
          	</td>
          </tr>
        </table></td>
    	
    	<td valign="top" style="padding-left:10px; border-left: 1px solid #000; width:290px;"><font face="Arial Unicode MS" style="font-size: 12pt"><b>Contact Us</b></font><font face="Arial Unicode MS" style="font-size: 9pt;"><br />
    	</font> <font face="Arial Unicode MS" style="font-size: 9pt">Please contact us directly on the details <br>
    	below</font><br/>
<p><font face="Arial Unicode MS" style="font-size: 9pt;"><b>Twenty Enterprise</b><br />
  Andrew White<br />
  Development Manager<br />
  Telephone: +61 3 8681 2229<br/>
  Email: <a href="mailto:awhite@mabcorp.com.au">awhite@mabcorp.com.au</a></font></p>
        </td>
    </tr>      
	<tr>
    	<td style="padding-left:20px; padding-right:10px; width:510px;">
        
        <table width="480" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse;">
        	<tr>
            	<td style="padding-left:10px;" colspan="3">
                <font face="Arial Unicode MS" style="font-size: 12pt; text-align: left;;"><b>Optional</b></font></td>
            </tr>
        	<tr>
            	<td style="padding-left:10px; padding-bottom:10px" colspan="3">
                <font face="Arial Unicode MS" style="font-size: 9pt; text-align: left;;">The following questions are optional:</font></td>
            </tr>
        	<tr>
            	<td width="200" style="padding-left:10px">
                <font face="Arial Unicode MS" style="font-size: 9pt; text-align: left;">Where did you hear about us?</font>
                </td>
                <td width="160" align="right" style="padding-right: 5px;">
                <apex:selectList value="{!Enquiry__c.Enquiry_Source__c}" style="width:160px" size="1">
                	<apex:selectOption itemValue="" itemLabel=" --- Select ---" />
                	<apex:selectOption itemValue="Agent" itemLabel="Agent" />
					<apex:selectOption itemValue="Catalogue" itemLabel="Catalogue" />
					<apex:selectOption itemValue="Domain.com.au" itemLabel="Domain.com.au" />
					<apex:selectOption itemValue="Facebook" itemLabel="Facebook" />
					<apex:selectOption itemValue="Google" itemLabel="Google" />
					<apex:selectOption itemValue="Newspaper - Herald Sun" itemLabel="Newspaper - Herald Sun" />
					<apex:selectOption itemValue="Newspaper - The Age" itemLabel="Newspaper - The Age" />
					<apex:selectOption itemValue="On-Site Signage" itemLabel="On-Site Signage" />
					<apex:selectOption itemValue="Outdoor Media" itemLabel="Outdoor Media" />
					<apex:selectOption itemValue="Radio" itemLabel="Radio" />
					<apex:selectOption itemValue="Realcommercial.com.au" itemLabel="Realcommercial.com.au" />
					<apex:selectOption itemValue="RealEstate.com.au" itemLabel="RealEstate.com.au" />
					<apex:selectOption itemValue="RealEstateVIEW.com.au" itemLabel="RealEstateVIEW.com.au" />
					<apex:selectOption itemValue="Referral" itemLabel="Referral" />
					<apex:selectOption itemValue="Word of Mouth" itemLabel="Word of Mouth" />
					<apex:selectOption itemValue="Other" itemLabel="Other" />
					<apex:selectOption itemValue="MAB CAB" itemLabel="MAB CAB" />
				</apex:selectList>
                </td>
                <td width="94">&nbsp;
                </td>
            </tr>
        	<tr>
            	<td width="200" valign="top" style="padding-left:10px">
                <font face="Arial Unicode MS" style="font-size: 9pt; text-align: left;">What office size are you <br>interested in?</font>
                </td>
                <td align="right" style="padding-right:5px;">
                <apex:selectList value="{!Enquiry__c.Office_Size__c}" style="width:140px" size="1">
                	<apex:selectOption itemValue="" itemLabel=" --- Select ---" />
                	<apex:selectOption itemValue="Under 50m2" itemLabel="Under 50m2" />
					<apex:selectOption itemValue="51m2 - 100m2" itemLabel="51m2 - 100m2" />
					<apex:selectOption itemValue="101m2 - 150m2" itemLabel="101m2 - 150m2" />
					<apex:selectOption itemValue="201m2 - 250m2" itemLabel="210m2 - 250m2" />
					<apex:selectOption itemValue="251m2 - 500m2" itemLabel="251m2 - 500m2" />
					<apex:selectOption itemValue="501m2 - 1000m2" itemLabel="501m2 - 1000m2" />
					<apex:selectOption itemValue="1001m2 - 2000m2" itemLabel="1001m2 - 2000m2" />
					<apex:selectOption itemValue="2000m2 +" itemLabel="2000m2 +" />
				</apex:selectList>
				
				<apex:inputHidden value="{!Enquiry__c.Webform_Source__c}" id="source" />
				
              </td>
              <td width="94"><apex:commandButton action="{!save}" value="Save" id="theButton" image="http://www.prmaustralia.com.au/MAB/submit.gif" style="border:0px;"/>
              	<script>
				        document.getElementById('{!$Component.theForm}').element['{!$Component.source}'].value = 'mabcorp';
				</script>
              </td>
          </tr>
          <tr>
          	<td colspan="3" style="padding-top:10px;">
          		<font FACE="Arial Unicode MS" COLOR="#333333" size="1">MAB conforms with relevant SPAM and Privacy laws. You can unsubscribe at any time. <br>
<a href="http://mabcorp.com.au/privacy/" target="_blank" style="color: #808080;">Click here to see our Privacy Policy</a></font></td>
          	</td>
          </tr>                    
        </table>
        
        </td>
        <td valign="bottom" align="right" style="padding-left:10px; padding-bottom: 10px; border-left: 1px solid #000;width: 290px;"><br/>
        <img src="http://www.prmaustralia.com.au/MAB/mab_logo.gif" align="right" style="margin-right:20px;">
        </td>
        
        
</table>

</body>
</apex:form>

</apex:page>

 

 

SteveBowerSteveBower
 document.getElementById('{!$Component.theForm}').element['{!$Component.source}'].value = 'mabcorp';


.elements <--- s = plural.



Don't ask me why I didn't suggest this earlier... brain freeze... :-)
tunedogtunedog

Thanks to both of you for your help with this.

 

Steve, I actually took the 's' out while trying to make it work. Very well spotted though!

 

The issue was actually that the Guest User profile didn't have field level access to the fields I was trying to write the hidden values to (D'oh!!).

Now that I have rectified that, the solution Jill provided works a treat.

 

Thanks again to you both!!