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
Phuc Nguyen 18Phuc Nguyen 18 

Make field read only based on user

Hello,
I have a apex component where I need to make some of the fields read only based on the logged in user.
<apex:outputfield value="{!Account.Agent__c}" />
Can I use an if statement like
If(($User.FirstName + " " +$User.LastName) != Text(Buyer__c)
make the agent field read only?
Thanks,
P
 
Agustin BAgustin B
HI Phuc, you could use this
<apex:outputfield value="{!Account.Agent__c}"> 
<apex:inlineEditSupport disabled="If(($User.FirstName + " " +$User.LastName) != Text(Buyer__c),true,false)"/>
</apex:outputfield>
Try that.

if it helps please mark as correct so the question can be closed.
 
Phuc Nguyen 18Phuc Nguyen 18
Agustin, thank you for the quick reply.  I received the following error:
Element type "apex:inlineEditSupport" must be followed by either attribute specifications, ">" or "/>"
Could it be the formula?
Thanks,
P
Agustin BAgustin B
Yes, I took the condition from your example try this for example to see if the first name works:
disabled="{!$User.FirstName + " " +$User.LastName != 'Text(Buyer__c)'}"

Please like and mark as correct if it helps you solve your issue.
good luck
Phuc Nguyen 18Phuc Nguyen 18
Still no luck. I just tried it with the fist name field and ended up with this eror:
The element type "apex:pageblocksection" must be terminated by the matching end-tag "</apex:pageblocksection>"
The Buyer__c is a picklist that is why I made I used TEXT.  Do I need the TEXT or sdo I need to put the field in {} ?
P
Agustin BAgustin B
But that is not in that part of the code, there is no apex:pageblocksection in this code.
Post the entire code to see.

Please if we are making progress like the comments in which you could advance, this encourage us to keep helping
Phuc Nguyen 18Phuc Nguyen 18
Sorry, the fields are within a pageblock.  did not realize this was relavant.  I removed the last name just to see if I can get it to save
<apex:pageblocksection id="prjdetail_pbs" title="Account Details " showheader="True" collapsible="true" columns="2" rendered="{!NOT(editing)}"> 

other outputfields....

<apex:outputfield value="{!Account.Agent__c}"> 
<apex:inlineEditSupport disabled="{!$User.FirstName != 'TEXT(Buyer__c)'}" />
</apex:outputfield>

 </apex:pageblocksection>

Thank you,
​​​​​​​P
Phuc Nguyen 18Phuc Nguyen 18
Hey Agustin
If I use this syntax it deploy:
<apex:inlineEditSupport disabled="{$User.FirstName  = TEXT(Buyer__c)}"/>

But If I add the last name it fails
<apex:inlineEditSupport disabled="{($User.FirstName + " " + $User.LastName) = TEXT(Buyer__c)}"/>
So we are getting close

Thanks,
P
 
Agustin BAgustin B
Try this: ($User.FirstName + ' ' + $User.LastName).
if not maybe you can create a field in your controller and join the firstName and LastName so you online check that field like FullName.
Please like the comments in which you make progress, and if you get your question answered please mark as the best
Phuc Nguyen 18Phuc Nguyen 18
Ok.  I added this to the controller
User usr = [SELECT ID, Name FROM User WHERE ID = :UserInfo.getUserId()];

When I added this to the VF page
<apex:inlineEditSupport disabled="{{!usr.Name} == TEXT(Buyer__c)}"/>
I get this error:
Unknown property 'MarketControllerExtension.usr'
Is it the way I am referencing it in the Controller?

Thanks,
P
 
Agustin BAgustin B
Hi Phuc, define your usr like this Public User usr{get; set;} below public class MarketControllerExtension.
And in the definition use only usr = [SELECT ID, Name FROM User WHERE ID = :UserInfo.getUserId()];
Something like this:
public class MarketControllerExtension{
    
    public User usr{get;set;}

    
    public MarketControllerExtension(ApexPages.StandardController controller){
usr = [SELECT ID, Name FROM User WHERE ID = :UserInfo.getUserId()];
}
}

 
Phuc Nguyen 18Phuc Nguyen 18
Agustin, No errors but I can still edit the field, even when I am logged in as another user.  The debug log is showing that the usr variable is me.  Is there a way to check the value of the Buyer Field on teh VF page?
Thanks for your help.
P
Agustin BAgustin B
How are you getting the buyer? Is inside Account? In that case, use a system.debug(acc.Buyer__c) after you query for the Account infromation.
For the validation use: <apex:inlineEditSupport disabled="{!usr.Name == (acc.Buyer__c)}"/>

This is assuming acc is the variable where you are saving the Account information.
Also if the acc.Buyer__c is not what you expect try using Text(acc.Buyer__c)

If not please show me the debug value for Buyer__c and what you want to show.
Phuc Nguyen 18Phuc Nguyen 18
Hey Agustin,
The Buyer__c is a picklist field with a few users names so I am not querying the field in the controller.  So if the value in the picklist is Agustin B and I am access the the VF pagge I should not be able to edit the Agent field.  I just thought of something.  The code resides in an apex component and not directly on the VF page.  Maybe that is the issue? 
Agustin BAgustin B
Yes that is an issue, you have to know the values you have in the Buyer__c so you can use the condition to disable the edit functionality of the Agent field.
If the apex component is calling the vf you can pass parameters, if you have the Buyer__c there then pass it as a parameter. If not you could query tan information into a Account acc variable.

Let me know.