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
sfg1sfg1 

Disable or Hide commandbutton for two public groups in visualforce page

I want to disable or hide commandbutton for users belong to two public groups "MMC: L1" and "MMC: L2". Please guide me.

VF page:
 <apex:pageBlockButtons >
   <apex:commandButton value="Save & Unlock"   action="{!saveAndUnlock}" styleClass="saveButton"/>
    </apex:pageBlockButtons>

Controller:
public PageReference saveAndUnlock() {
        
        if(!isNotAllwToSave())
            return null;

        if(!isOkToSave())
            return null;

        // update medical chart record...
        this.codingChart.Record_Lock__c = false;
        codingChart.Record_Lock_User__c = null;

        try {
            this.codingChart.Record_Lock__c = false;
            this.codingChart.Record_Lock_User__c = null;
            this.codingChart.Status__c = selectedStatusOption;
            this.codingChart.Saved_Record_Status_Check__c = false; 
            update this.codingChart;
            saveDxCodes();

        } catch(Exception ex) {
            Apexpages.addMessages(ex);
            return null;
        }
        deleteWorkingCodes();
       return new PageReference('/apex/MC_ChartsTab'); //Redirect to charts queue
    }
Best Answer chosen by sfg1
Girish Nallamothu 20Girish Nallamothu 20
In the constructor  of the controller please try this 

public NameOfController(){                         
       
        advID = UserInfo.getUserId(); // gets ID of person logged in    
        //renders Email Button
        List<GroupMember> getGroup = [SELECT UserOrGroupId FROM GroupMember WHERE UserOrGroupId =:advid and group.name = 'Group Name'];
        if (getGroup.size()>0){
            showButton=true;
        }else{
            showButton=false;
        }
        
     }

Use the showButton in rendered attribute of the button

<apex:commandButton value="Save & Unlock"   action="{!saveAndUnlock}" styleClass="saveButton" rendered="{!showButton}"/> 

All Answers

Girish Nallamothu 20Girish Nallamothu 20
In the constructor  of the controller please try this 

public NameOfController(){                         
       
        advID = UserInfo.getUserId(); // gets ID of person logged in    
        //renders Email Button
        List<GroupMember> getGroup = [SELECT UserOrGroupId FROM GroupMember WHERE UserOrGroupId =:advid and group.name = 'Group Name'];
        if (getGroup.size()>0){
            showButton=true;
        }else{
            showButton=false;
        }
        
     }

Use the showButton in rendered attribute of the button

<apex:commandButton value="Save & Unlock"   action="{!saveAndUnlock}" styleClass="saveButton" rendered="{!showButton}"/> 
This was selected as the best answer
sfg1sfg1
Thanks Girish. I have one more issue. Please guide.
  User A is member of public group L1
  User B is member of public group L1,L2
  User C is member of public group L1,L2,L3
  User D is member of public group L1,L2,L3,L4
  
  
  I am able to hide for User A and B. But User C not able to see button. I want to hide only for member belong Groups
  L1 and L2. I want to display button even if user is part of group L1,L2,L3 AND L4. Please guide me in this.

My Query:
  List<GroupMember> getGroup = [SELECT UserOrGroupId FROM GroupMember WHERE UserOrGroupId = :advID and (group.name IN ('L3','L4')) and (group.name NOT IN ('L1','L2'))];

Also i have Boolean variable for users L1User,L2User,L3User and L4User in Controller.