1

On my page I have a picklist that contains the names of all active users for the org. I'd like to add a checkbox that, when checked, repopulates the picklist with inactive users instead. How can I accomplish this?

Apex for populating the list:

List<User> actUserList = [SELECT user.name, user.id FROM User WHERE profile.userlicense.name != 'Chatter Free' AND User.isActive = true];

    public List<selectOption> getFromList(){
    List<SelectOption> fromOptions = new List<SelectOption>();

    actUserList.sort();
    for(User U : actUserList){
        fromOptions.add(new SelectOption(u.Id,u.Name));
    }
    return fromOptions;
}

visualforcepage:

<apex:page id="TransferPage" controller="recordTransferController" showHeader="true" title="Mass Record Transfer" >
<apex:pageMessages id="TheMessage" />
<apex:form >
    <apex:pageBlock >
        <apex:pageblockButtons >
            <apex:commandButton action="{!transferRecords}" value="Transfer"/>
        </apex:pageblockButtons>
        <apex:pageBlockSection title="Select Record Types">
        <apex:selectCheckboxes value="{!RecordTypes}"  label="Record Types to Transfer">
            <apex:selectOptions value="{!recordTypeList}"/>
         </apex:selectCheckboxes>
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Select user to search and user to receive records">
        <apex:selectList id="fromUserList" value="{!FromIds}" multiselect="false" size="10" label="Transfer Records From">
            <apex:selectOptions value="{!FromList}"/>
        </apex:selectList>
        <apex:selectList value="{!ToId}" multiselect="false" size="10" label="Transfer Records To">
            <apex:selectOptions value="{!ToList}"/>
        </apex:selectList>
        </apex:pageBlockSection>    
    </apex:pageBlock>
</apex:form>

0

I would recommend using a commandButton strictly to avoid having to use javascript actionFunctions to rerender the page on a input's onClick action.

Each time the user clicks the input, you'll need to rerender the page, to display the new options for the user to interact with. When the user clicks either of these commandButtons, a boolean inside the controller will be switched to a different value, and then the outputPanel will be refreshed. The refreshed controller will then run getFromList, and will pull a list of users based on the boolean value specified in the controller. The buttons used require immediate="true" to prevent form submission when trying to switch over the selectOption list.

<apex:outputPanel id="userSelection"> 
    <apex:selectCheckboxes value="{!userSelection}">
        <apex:selectOptions value="{!FromList}"/>
    </apex:selectCheckboxes>
    <apex:outputPanel rendered="{!UsingActiveUsers}">
        <apex:commandButton action="{!SwitchToInactiveUsers}" title="Switch to Inactive Users" immediate="true" reRender="userSelection" />
    </apex:outputPanel>
    <apex:outputPanel rendered="{!UsingInactiveUsers}">
        <apex:commandButton action="{!SwitchToActiveUsers}" title="Switch to Active Users" immediate="true" reRender="userSelection" />
    </apex:outputPanel>
</apex:outputPanel>

Inside your controller, a few new values are used. UsingActiveUsers and UsingInactiveUsers are just getters for a private boolean, used for readability in the page. SwitchToInactiveUsers and SwitchToActiveUsers are used to toggle this value, and refresh the page. Once your query is moved into the getFromList function, we can bind inactiveUsers to the query, and ensure the value used is always the correct value.

public Boolean UsingActiveUsers { get { return !inactiveUsers; } } 
public Boolean UsingInactiveUsers { get { return inactiveUsers; } } 

private Boolean inactiveUsers; 

public SomeConstructor() {
    inactiveUsers = false; 
}

public PageReference SwitchToActiveUsers() {
    inactiveUsers = false; 

    return null;
}

public PageReference SwitchToInactiveUsers() {
    inactiveUsers = true; 

    return null;
}

public List<SelectOption> getFromList() {
    List<User> userList = [SELECT Name, Id FROM User WHERE Profile.UserLicense.Name != 'Chatter Free' AND User.isActive := inactiveUsers];
    List<SelectOption> fromOptions = new List<SelectOption>();

    for (User u:userList) {
        fromOptions.add(new SelectOption(u.Id, u.Name));
    }

    return fromOptions;
}

This could be modified to call a javascript actionFunction instead of using commandButtons, but this should serve as an example of the logic required to switch values on the page based on user interaction with the visualforce page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.