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
EagerToLearnEagerToLearn 

list controller with free form input field

I am trying to write a list controller that will get a list of users showing two fields.  The username and a text field as input on the visualforce page.  When I click the "GO" button I want to call a method where I will use the list of users with the value in the text field to do some further work.  Can someone help me with a block code for this and VF page.  I think I need a wrapper class for the text field but I an't see to make sense from other examples as I don't see any were the text field is not a bind to any field on the user object.
Thanks for any help you can provide to get me started.
Sampath SuranjiSampath Suranji
Hi EagerToLearn,
As my understanding your requirement Please try this,
VF page
<apex:page controller="myController">
    <apex:form>
        <apex:pageBlock title="Users" mode="edit">
            <apex:pageBlockButtons>
                <apex:commandButton action="{!go}" value="Botton Go"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="User Details" columns="2">
                
<!-- if you are using any other custom object with user lookup, please replace {!userDetails} by ' {!getUserDetailsCustom}' -->
                <apex:repeat value="{!UserDetails}"  var="u">
                    <apex:outputField label="User Name" value="{!u.userName}"/>
                    <apex:inputField value="{!u.MyTextField__c}"/>
                </apex:repeat>
                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 Controller class
public class myController {
    
    // if you are using standard user object
    public LIST<user> getUserDetails()
    {
        LIST<user> objUserList = [select username,MyTextField__c from user];
        return objUserList;
    }
    
    // if you are using any custom object which has user lookup user object
    public LIST<YourCostomObjectName__c> getUserDetailsCustom()
    {
        LIST<YourCostomObjectName__c> objUserList = [select user__r.name ,MyTextField__c from YourCostomObjectName__c];
        return objUserList;
    }
    
    public void go(){
        
    }

}

Regards
EagerToLearnEagerToLearn
Thank you so much for the jump start.  I should have been more clear about the "text field not bind to" statement  Its not a drect tie to the User object from the VF page.  I did some digging and found some wrapper class examples that got me to what I need.  For sharing reason I have put the code below.  It is still in the works but the bottom line it does what I need to do which is allow for our process user's passwords to be changed to something different after a sandbox refresh.  Not sure if others have this type of concern but we don't want the passwords to be the same as production once they exist in a sandbox.

I need to create the test class next and usually struggle a bit on that put I will get it - just not as fast as I would like.  Any further anhancedment suggestions or pointers were you see wholes in it I would appeciate the help/collaboration. 


Visualforce:
 
<apex:page controller="SandboxRefreshPwdChgController" title="Password Changer">
<apex:form >    
    <apex:pageBlock mode="maindetail" title="Active Process Users *.process">
        <apex:pageMessage summary="Enter passwords for users you want to change then click the 'Update Passwords' button" severity="INFO" strength="3"/>
        <apex:pageMessages />
        <apex:pageBlockTable value="{!wrappedObjects}" var="w">
            <apex:column headerValue="Username" value="{!w.rowCounter}">
                <apex:facet name="header">#</apex:facet>
            </apex:column>
            <apex:column headerValue="Successful Update" width="8" >
                <apex:outputText value="{!w.successfulChange}"/>
            </apex:column>
            <apex:column headerValue="Password" width="8">
                <apex:inputText value="{!w.pwd}"/>  
            </apex:column> 
            <apex:column value="{!w.user.username}"/>
            <apex:column value="{!w.user.email}" />
            <apex:column headerValue="Profile Name" value="{!w.user.Profile.Name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    <apex:commandButton action="{!UpdatePasswords}" value="Update Passwords"/>
</apex:form>
</apex:page>




Controller:
 
public with sharing class SandboxRefreshPwdChgController {

    public void UpdatePasswords() {
        Integer pwdUpdateCount = 0;
        //Loop through the wrapperObjects and update the passwords
        for(userWrapper u : wrappedObjects) {            
            System.debug('u.user.id: ' + u.user.id);
            System.debug('u.user.username: ' + u.user.username);
            System.debug('u.pwd: ' + u.pwd);
            //skip trying to set the password if Password on the visualforce page is blank
            if(u.pwd != '') {
                u.successfulChange = 'No';
                try {
                    System.setPassword(u.user.Id, u.pwd);
                    u.successfulChange = 'Yes';
                    pwdUpdateCount++;
                } catch(System.Exception e) {
                    ApexPages.addMessages(e);      
                }
            }
        }
        if(pwdUpdateCount == 0) {
            apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.Error,'No Users password(s) where entered or saved successfully!'); 
            ApexPages.addMessage(msg);
        }
    }

    //list for holding the wrapped users and Integers to be displayed on the page 
    public List<userWrapper> wrappedObjects = new List<userWrapper>(); 
    
    //contructor for the page
    public SandboxRefreshPwdChgController() {
        Integer i = 0; //integer for tracking the row counter value we want to pass back in the wrapper
        for (user c : [SELECT id, username, email, profile.Name
                       FROM user 
                       WHERE isActive = TRUE AND Profile.Name != 'System Administrator' AND
                             username LIKE '%.process%'
                       ORDER BY username]) { //start looping through users
            i++; //increment the integer
            userWrapper wrappedObject = new userWrapper(c, i); //wrap the user with the Integer, which calls the userWrapper class below
            wrappedObjects.add(wrappedObject); //add the wrapper object to our list
        }
    }
    
    //allows for Visualforce page to request the list of userWrapper records
    public List<userWrapper> getWrappedObjects() {
        return wrappedObjects; //return the list of userWrapper records
    }
    
    //wrapper class definition
    public class userWrapper {
        
        public user user {get; set;} //user object
        public Integer rowCounter {get; set;} //row Integer
        public String pwd {get; set;}
        public String successfulChange {get; set;}
        
        //constructor for wrapper
        public userWrapper(user passeduser, Integer passedInteger) {
            user = passeduser; //assign user
            rowCounter = passedInteger; //assign row counter            
        }
    }

}

 
EagerToLearnEagerToLearn
I posted this question here:
https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=9060G0000005TmKQAU


but realized that it is kind of associated to the same work!  I am getting an error List index out of bound: 0 from my test class.  The page and code is working as expected but I can't get the test class issue resolved!  Appreciate any support possible.