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
Will EdwardsWill Edwards 

What's wrong with this use of inputHidden?

I'm trying to fill a hidden custom field through a parameter provided via the URL in a Visualforce page on a Force.com site that creates leads. I'm getting the following error: "Error: Literal value is required for attribute id in apex:inputHidden in NomineeRegistration at line 38 column 99."
Here's my Apex:
 
public class myWeb2LeadExtension {

private final Lead weblead;

public myWeb2LeadExtension(ApexPages.StandardController
                            stdController) {
   weblead = (Lead)stdController.getRecord();
}

 public PageReference saveLead() {
   try {
   insert(weblead);
   }
   catch(System.DMLException e) {
       ApexPages.addMessages(e);
       return null;
   }
   PageReference p = Page.ThankYou;
   p.setRedirect(true);
   return p;
 }

And the highlights of the VF page:
 
<apex:page standardController="Lead"
       extensions="myWeb2LeadExtension"
       title="Register for Digital Accelerator" showHeader="false"
       standardStylesheets="true">

<apex:composition template="{!$Site.Template}">
<apex:define name="body">
<apex:form >
<apex:messages id="error"
               styleClass="errorMsg"
               layout="table"
               style="margin-top:1em;"/>
  <apex:pageBlock title="" mode="edit">
    <apex:pageBlockButtons >
       <apex:commandButton value="Submit"
                           action="{!saveLead}"/>
    </apex:pageBlockButtons>
    <apex:pageBlockSection title="Register for Digital Accelerator"
                           collapsible="false"
                           columns="1">
    <div class = "requiredInput">
    <div class = "requiredBlock"></div>
     <apex:inputField value="{!Lead.FirstName}" required="true"/>
     <apex:inputField value="{!Lead.LastName}" required="true"/>
     <apex:inputField value="{!Lead.Nickname__c}" required="false"/>
     <apex:inputField value="{!Lead.Title}" required="true"/>
     <apex:inputField value="{!Lead.Email}" required="true"/>
     <apex:inputField value="{!Lead.Company}" required="true"/>
     <apex:inputField value="{!Lead.Phone}" required="true"/>
     <apex:inputField value="{!Lead.Mailing_Address__c}" required="true"/>

    </div>
    <apex:inputHidden id="{!Lead.Referred_By__c}"
                      value="{!$CurrentPage.parameters.Referred"/>

    </apex:pageBlockSection>
 </apex:pageBlock>
Best Answer chosen by Will Edwards
Chris Gary CloudPerformerChris Gary CloudPerformer
Quite Literally, The parser is telling you - "Sorry, you cannot assign a dynamic value to the 'ID' attribute of a hidden input field".  If this was a regular input element, like '<input type="hidden" id="{!Your dynamic value}" value="{!whatever}"/>' - you could do that, but since you are using the <apex:inputHidden/> element, you cannot dynamically assign the Id value.  If you need the "Referred_By__c" value, just consider using two hidden fields in the form, not just one.  Example :
<apex:inputHidden id="ReferencedByHiddenField" value="{!Lead.Referred_By__c}"/>
<apex:inputHidden id="OtherValueNeeded" value="{!theotherdynamicvalueIneed}"/>

 

All Answers

Chris Gary CloudPerformerChris Gary CloudPerformer
Quite Literally, The parser is telling you - "Sorry, you cannot assign a dynamic value to the 'ID' attribute of a hidden input field".  If this was a regular input element, like '<input type="hidden" id="{!Your dynamic value}" value="{!whatever}"/>' - you could do that, but since you are using the <apex:inputHidden/> element, you cannot dynamically assign the Id value.  If you need the "Referred_By__c" value, just consider using two hidden fields in the form, not just one.  Example :
<apex:inputHidden id="ReferencedByHiddenField" value="{!Lead.Referred_By__c}"/>
<apex:inputHidden id="OtherValueNeeded" value="{!theotherdynamicvalueIneed}"/>

 
This was selected as the best answer
Chris Gary CloudPerformerChris Gary CloudPerformer
Hi Will,
Just checking in to see if you got your issue resolved with your inputHidden element. If so, and my answer helped, please mark as 'Best Answer'. if you solved this on your own, please comment on your solution, and mark your comment as 'Best Answer' so that this can be marked as solved. Keep the community alive! Thanks!
Will EdwardsWill Edwards
Thanks, Chris.
Will EdwardsWill Edwards
I tried to "Best Answer" it, but it wouldn't let me.