+ Start a Discussion
SoozeeSoozee 

Render Button based on user's public group membership

I want to render a button based on a user's public group membership.

For example, if user is member of Email Administrator public group, they should be able to see an 'Email Button' at the bottom of the page.

 

Here is the VF Page:

 

<apex:form>
... lots of other stuff...

<apex:commandButton value="Email Letters" onclick="if (confirmation()) addEmailLettersJS(); return false;" rendered="{!renderButton}"/>
</apex:form>

 Here is the controller:

 

public boolean renderButton() {
    
    System.DEBUG('********* renderButton');
       List<GroupMember> getGroup = [SELECT UserOrGroupId FROM GroupMember WHERE UserOrGroupId =:advid and group.name = 'EOTL - Email Admin'];
       //GroupMember[] getGroup=[SELECT UserOrGroupId FROM GroupMember WHERE UserOrGroupId =:advid and group.name = 'EOTL - Email Admin'];

        if (getGroup[0].UserOrGroupId == advID){
            showButton=true;
        }else{
            showButton=false;
        }
        System.DEBUG('********* value of show button = '+showButton);
      return showButton;
    }

 

The {!renderButton} on the page ALWAYS returns false, even when the user is in the group.

Also, the debug never writes the console.

 

Help!?!

Best Answer chosen by Admin (Salesforce Developers) 
SoozeeSoozee

Ok - I was able to resolve this.

 

Instead of calling the !renderButton function in the controller, I moved the query into the constructor like 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 = 'EOTL - Email Admin'];
        if (getGroup.size()>0){
	        showButton=true;
	    }else{
			showButton=false;
        }
        
        populatePage();
         
        
    }

 And then referenced the showButton variable on the VF page, like this:

<apex:commandButton value="Email Letters" onclick="if (confirmation()) addEmailLettersJS(); return false;" rendered="{!showButton}"/> 

 Thanks!

 

Suzie

All Answers

bob_buzzardbob_buzzard

Is your user a direct member of the group, or in a subgroup that is included?

 

Also, how is advId determined?

SoozeeSoozee

Hi -

Member is a direct member of the group.

and

advID = UserInfo.getUserId();

 

Thank you!

bob_buzzardbob_buzzard

Hmm.  That sounds correct.  Have you tried running this in the developer console with a hardcoded id to verify the query?

SoozeeSoozee

Hi - I ran it in Force.com Explorer.  It returns the ID of the user if the user is a member.

SoozeeSoozee

Hi - It seems as though the !renderButton method/function is not running.  Do I need to reference it somewhere else to force it to run when the page loads?

 

SoozeeSoozee

Ok - I was able to resolve this.

 

Instead of calling the !renderButton function in the controller, I moved the query into the constructor like 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 = 'EOTL - Email Admin'];
        if (getGroup.size()>0){
	        showButton=true;
	    }else{
			showButton=false;
        }
        
        populatePage();
         
        
    }

 And then referenced the showButton variable on the VF page, like this:

<apex:commandButton value="Email Letters" onclick="if (confirmation()) addEmailLettersJS(); return false;" rendered="{!showButton}"/> 

 Thanks!

 

Suzie

This was selected as the best answer