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
LaurensBLaurensB 

Set value for a field in VF page

Hi,

I created a button that calls a VF page with 2 fields on it:
- Closure Code
- Resolution
But I want to set the field Status to 'Closed' without displaying it on the VF page or that a user needs to select it themselves.
How does one do that? Can I do that straight in the VF page? I certainly hope so :-)
Any help on this appreciated.

Best Answer chosen by LaurensB
AndersonAdminAndersonAdmin
Hi,
There are several ways you could accomplish this, but one of the easiest is to simply show the Status field, but only include one option that is defaulted on the page and the user won't be able to modify the choice in the dropdown.
<apex:page...>
  <apex:form>
    <apex:inputField value="{!object.ClosureCode__c}"/>
    <apex:inputField value="{!object.Resolution__c}"/>
    <apex:selectList value="{!object.Status}" size="1">
      <apex:selectOption itemValue="Closed" itemLabel="Closed"/>
    </apex:selectList>
  </apex:form>
</apex:page>
Obviously remember to replace the "object" with your proper object name.

Hope this helps!
 

All Answers

AndersonAdminAndersonAdmin
Hi,
There are several ways you could accomplish this, but one of the easiest is to simply show the Status field, but only include one option that is defaulted on the page and the user won't be able to modify the choice in the dropdown.
<apex:page...>
  <apex:form>
    <apex:inputField value="{!object.ClosureCode__c}"/>
    <apex:inputField value="{!object.Resolution__c}"/>
    <apex:selectList value="{!object.Status}" size="1">
      <apex:selectOption itemValue="Closed" itemLabel="Closed"/>
    </apex:selectList>
  </apex:form>
</apex:page>
Obviously remember to replace the "object" with your proper object name.

Hope this helps!
 
This was selected as the best answer
LaurensBLaurensB
Hi,

That worked very well. Is there anyway I can hide the selectlist. I have set it to disabled now but I would prefer to hide it. I tried rendered="false" but that does not work it ignores the field then.
Thank you in advance.
AndersonAdminAndersonAdmin
Hi there,
Yes, there is - simply modify the style attribute of the selectlist element:
 
<apex:selectList value="{!object.Status}" size="1" style="visibility:hidden">

The complete code would then be something like:
 
<apex:page standardController="Account">
  <apex:form>
    <apex:commandButton value="Save" action="{!Save}"/>
    <apex:inputField value="{!object.ClosureCode__c}"/>
    <apex:inputField value="{!object.Resolution__c}"/>
    <apex:selectList value="{!object.Status}" size="1" style="visibility:hidden">
      <apex:selectOption itemValue="Closed" itemLabel="Closed"/>
    </apex:selectList>
  </apex:form>
</apex:page>

I just tested this and it works great.

Hope this helps!
LaurensBLaurensB
Hi,

Again brilliant, but anyway to hide the label?
 
LaurensBLaurensB
Ah, found it myself. Added label="" to it.