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
uzairuzair 

How to hide a field in VF page depending upon accessed from objects.

Hi All,

 

I have created a visualforce page consisting of four fields A, B, C and D.

I'm accessing this page from Accounts and Opportunities.

 

What I require is :

When i'll access this page from Accounts the field 'C' should become read only and the field 'D' should be Hidden.

But when i'll access this page from Opportunities all the fields should be editable and visible

 

any help regarding this will be appreciated.

 

Thanks in Advance.

Best Answer chosen by Admin (Salesforce Developers) 
ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi Uzair,

1)  Maintain 2 fields for 'C' i.e. one as <apex:inputText> and other as <apex:outputText>

2) Add rendered property to both the fields of 'C' as well the one for 'D'

 

e.g.

Controller

 

public Boolean isAccount {get; set;}

public Boolean isOppty {get; set;}

 

if( page from Accounts)

{

isAccount = true;

isOppty = false;

}

else

{

isAccount = false;

isOppty = true;

}

 

Page //launched from Account tab

 

<apex:inputText value="{!A}" />

<apex:inputText value="{!B}" />

<apex:inputText value="{!C}"  rendered="{!isOppty}" /> //will not be rendered since isOppty is false

<apex:outputText value="{!C}"  rendered="{!isAccount}" /> //will be rendered since isAccount is true

<apex:inputText value="{!D}"  rendered="{!isOppty}" /> //will not be rendered since isOppty is false

 

Hope this helps.

All Answers

bob_buzzardbob_buzzard

You'd normally achieve this via the rendered attribute of the input field.  This attribute can contain a formula for straightforward conditions, or invoke a getter on the controller if more complex logic is required.

Pradeep_NavatarPradeep_Navatar

when you make properties of field in apex then either you can change display according to your requirement or use rendered property in component by this way:

 

         display:none:// for D field

         display:readonly: // for c field in case of account

 

Hope this helps.

uzairuzair

Could you please provide a sample example.

 

Thanks in advance.

ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

Hi Uzair,

1)  Maintain 2 fields for 'C' i.e. one as <apex:inputText> and other as <apex:outputText>

2) Add rendered property to both the fields of 'C' as well the one for 'D'

 

e.g.

Controller

 

public Boolean isAccount {get; set;}

public Boolean isOppty {get; set;}

 

if( page from Accounts)

{

isAccount = true;

isOppty = false;

}

else

{

isAccount = false;

isOppty = true;

}

 

Page //launched from Account tab

 

<apex:inputText value="{!A}" />

<apex:inputText value="{!B}" />

<apex:inputText value="{!C}"  rendered="{!isOppty}" /> //will not be rendered since isOppty is false

<apex:outputText value="{!C}"  rendered="{!isAccount}" /> //will be rendered since isAccount is true

<apex:inputText value="{!D}"  rendered="{!isOppty}" /> //will not be rendered since isOppty is false

 

Hope this helps.

This was selected as the best answer
uzairuzair

Hi ipsita.biswas,

 

Thanks for your reply, it worked for me.