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
tengeltengel 

Lock fields/make read-only if condition met

I have what appears to be a pretty simply requirement but I am really struggling with this one...

I have a custom object and am overriding the standard Edit page with a Visualforce page. This object has custom checkbox field, Finalized__c.

I'm looking to do either one of these two things, and the preferable method would be avoid an apex controller, if possible:
  • IF Finalized__c = TRUE, make the whole page readOnly;
  • IF Finalized__c = TRUE, make field A__c, B__c, and C__c read only.
Any ideas? Greatly appreciated.
Best Answer chosen by tengel
Virendra ChouhanVirendra Chouhan
Hi tengel,

It was my mistake to read unclearly.
now i understand what you want.
i did it check this -->

Here i take my custom object name is Object_A__c and some custom fields.
If Object_A__c.Finalized__c is checked then all the fields are read only.
and if not then it'll be editable.

<apex:page standardController="Object_A__c">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:inputField value="{!Object_A__c.name}" rendered="{!NOT(Object_A__c.Finalized__c)}"/>
            <apex:outputField value="{!Object_A__c.name}" rendered="{!Object_A__c.Finalized__c}"/>
            <apex:inputField value="{!Object_A__c.Email__c}" rendered="{!NOT(Object_A__c.Finalized__c)}"/>
            <apex:outputField value="{!Object_A__c.Email__c}" rendered="{!Object_A__c.Finalized__c}"/>
             <apex:inputField value="{!Object_A__c.Finalized__c}" />
            
        </apex:pageBlockSection>
 
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!quickSave}" value="Save"/>
           
        </apex:pageBlockButtons>
 
    </apex:pageBlock>
</apex:form>
  
</apex:page>

let me know if it helped you.

Regards
Virendra
version7.7@hotmail.com (mailto:version7.7@hotmail.com)

All Answers

Virendra ChouhanVirendra Chouhan
Hi Tengel,
Are you sure in both conditions Finalized__c 's value is TRUE ???

Regards
VIrendra
tengeltengel
Hi Virendra, sorry if my first post was unclear.

I meant that either of those solutions would be acceptable. So the full IF-statement for the first proposed solution would be: IF Finalized_c = TRUE, make the page read only, otherwise don't make the page read only. For the second proposed solution, it would: IF Finalized__c = TRUE, make field A__c read only, otherwise allow field A_c to be edited.
Virendra ChouhanVirendra Chouhan
Hi tengel,

It was my mistake to read unclearly.
now i understand what you want.
i did it check this -->

Here i take my custom object name is Object_A__c and some custom fields.
If Object_A__c.Finalized__c is checked then all the fields are read only.
and if not then it'll be editable.

<apex:page standardController="Object_A__c">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:inputField value="{!Object_A__c.name}" rendered="{!NOT(Object_A__c.Finalized__c)}"/>
            <apex:outputField value="{!Object_A__c.name}" rendered="{!Object_A__c.Finalized__c}"/>
            <apex:inputField value="{!Object_A__c.Email__c}" rendered="{!NOT(Object_A__c.Finalized__c)}"/>
            <apex:outputField value="{!Object_A__c.Email__c}" rendered="{!Object_A__c.Finalized__c}"/>
             <apex:inputField value="{!Object_A__c.Finalized__c}" />
            
        </apex:pageBlockSection>
 
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!quickSave}" value="Save"/>
           
        </apex:pageBlockButtons>
 
    </apex:pageBlock>
</apex:form>
  
</apex:page>

let me know if it helped you.

Regards
Virendra
version7.7@hotmail.com (mailto:version7.7@hotmail.com)
This was selected as the best answer
tengeltengel
I had a feeling it would come down to rendering. Thanks for the quick solution, this works perfectly.