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
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Visual force page readonly

Hi, 

 I want to make readonly to visualforce page for some profiles in salesforce please suggest me how to make readonly 

  Profile name ( GHC, Program Admin, Finance) Only Progam Admin should be able to editable other profiles must be read only 

Thanks
Sudhir
Anirudh SinghAnirudh Singh
Hello Sudhir,

There is a readOnly attribute in apex:page tag, but you cannot put any condition on like mode='edit' or mode='readOnly', it only accepts true or false values.

So, we cannot make the Visualforce Page read-only or editable according to Profile Name.

But we can make the fields read-only or editable on the Visualforce Page according to the Profile Name.
For achieving this, you can create two pageBlockSections.
Put rendering condition on both of them.
In one pageBlockSection put the inputFields and in another outputFields.

I have written a dummy Page and Controller. Please see below:
<apex:page standardController="Account" extensions="ReadOnly_Controller" action="{!modeSet}">
    <apex:form>
    	<apex:pageBlock>
        	<apex:pageBlockSection rendered="{!mode='edit'}">
            	<apex:inputField value="{!acc.Name}"/>
                <apex:inputField value="{!acc.Type}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection rendered="{!mode='readOnly'}">
            	<apex:outputField value="{!acc.Name}"/>
                <apex:outputField value="{!acc.Type}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class ReadOnly_Controller
{
    public Account acc{get; set;}
    
    public String mode{get; set;}
    
    public ReadOnly_Controller(ApexPages.StandardController stdController)
    {
        //...do some processing as needed...
    }
    
    public void modeSet()
    {
        Profile profileRec=[SELECT Name FROM Profile WHERE Id=:UserInfo.getProfileId()];
        if(profileRec.Name=='Program Admin')
        {
            mode='edit';
        }
        else
        {
            mode='readOnly';
        }
    }
}

Please let me know if this helps.
If yes, please mark the Question as Solved.


Thanks and Regards,
Anirudh Singh
Vj@88Vj@88
You just need to set the fields as Red Only for all the profiles in the Field Security. VF page automatically converts all those Input fields as Read Only based on the User's profile
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks for your reply this is the visualforace page and controller am using please suggest me how to change to read only and edit mode 
 
<apex:page showheader="false"  sidebar="false" controller="MultiRow"   html-width="100%" title="Edit Expence Lines" >

<apex:includeScript value="https://code.jquery.com/jquery-1.11.3.min.js" />

<script>
$j = jQuery.noConflict();
$j(document).ready(function() {
$j('.rjchk').on('change',function(){
   if($j(this).prop('checked')) {
    $j(this).closest('tr').find('.amountTxt').val(0);
   }
});

$j('.amountTxt').on('change',function(){
//if($j(this).val() !=  $j(this).closest('tr').find('.subAmt').text()){
     $j(this).closest('tr').find('.rjchk').prop('checked',true);
//}

});


   });




</script>

 <p align="center"><b>Edit Expense Lines</b> </p>

   
<apex:form >
        <apex:pageblock >
        <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockButtons >               
                <apex:commandButton value="Save" action="{!Save}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1">
                <apex:dataTable value="{!Expenses_Lines}" var="c" border="1" rowClasses="odd,even" styleClass="tableClass" cellpadding="3">
                    <apex:column >
                        <apex:facet name="header">Reject</apex:facet>
                        <apex:inputCheckbox value="{!c.Checked}" styleClass="rjchk" />
                        <!-- <apex:commandLink value="Check" action="{!URLFOR($action.Contact.Edit, c.ID)}" target="_blank" class="rjchk" /> -->
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">Expenses Lines Name</apex:facet>
                        <apex:outputText value="{!c.Name}"/>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">Date</apex:facet>
                        <apex:outputText value="{!c.Dates}"/>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">Expense Type</apex:facet>
                        <apex:outputText value="{!c.ExpenseType}"/>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">Submitted Amount</apex:facet>
                        <apex:outputText value="{!c.SubmittedAmount}" styleClass="subAmt" />
                    </apex:column>
                    
                    <apex:column >
                        <apex:facet name="header">Approved Amount</apex:facet>
                        <apex:inputText value="{!c.ApprovedAmount}" styleClass="amountTxt"/>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">Comments</apex:facet>
                        <apex:inputTextArea value="{!c.Comments}" cols="60" rows="3" />
                    </apex:column>
                </apex:dataTable>
            </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>
     
</apex:page>
 
public class MultiRow {
    public List<multiRowContact> Expenses_Lines { get; set; }
 
    public MultiRow() {
        LoadData();        
    }
 
    public PageReference Save() {
        for (multiRowContact MRC : Expenses_Lines) {
            if(MRC.ApprovedAmount > MRC.SubmittedAmount){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Approved Amount is greater than Submitted Amount for '+MRC.Name)); 
                return null;
            }
            MRC.Save();
        }
        LoadData();
        return null;
    }
    private void LoadData() {
        Expenses_Lines = new List<multiRowContact>();
        for (List<Expenses_Lines__c> cs : [SELECT c.ID, c.Name, c.Comments__c,Approved_Amount__c,c.Date__c,c.Expense_Type__c,c.Amount__c FROM Expenses_Lines__c c WHERE c.Expenses__c = :  ApexPages.currentPage().getParameters().get('id')]) {
            for (Expenses_Lines__c c : cs) {
                multiRowContact MRC = new multiRowContact();
                MRC.ID = c.ID;
                MRC.Checked = false;
                MRC.Name = c.Name;
                MRC.Comments = c.Comments__c;
                MRC.ApprovedAmount  = c.Approved_Amount__c;
                MRC.Dates = c.Date__c;                
                MRC.SubmittedAmount = c.Amount__c;
                MRC.ExpenseType = c.Expense_Type__c;
                Expenses_Lines.add(MRC);
            }
        }
    }
 
    private class multiRowContact {
        public String ID { get; set; }
        public String Name { get; set; }
        public String Comments { get; set; }
        public Date Dates { get; set; }
        public String ExpenseType { get; set; }
        public Double  SubmittedAmount { get; set; }
        public Double  ApprovedAmount { get; set; }
        public Boolean Checked { get; set; }
        public void Save() {
            if (Checked) {
                System.debug('Saving...ID: ' + ID);
                Expenses_Lines__c c = [SELECT c.Comments__c,Approved_Amount__c FROM Expenses_Lines__c c WHERE c.ID = :ID LIMIT 1];
                c.Comments__c = Comments;
                c.Approved_Amount__c = 0;                
                update c;
            }
            else
            {          
              System.debug('Saving...ID: ' + ID);  
               Expenses_Lines__c c = [SELECT c.Comments__c,Approved_Amount__c FROM Expenses_Lines__c c WHERE c.ID = :ID LIMIT 1];
                c.Comments__c = Comments;
                c.Approved_Amount__c = ApprovedAmount;
                Checked = true;
                update c;
            }
        }
    }
}