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
RiflemanRifleman 

Displaying Return Values of Functions in SelectList

In my controller, I have this function, which calls a utility function to get all subordinate users to the currently logged in user:

public string getUsers() {

Set<Id> userlist = new Set<Id>();
userlist.add(UserInfo.getUserId());
userlist.addAll(RoleUtils.getRoleSubordinateUsers(UserInfo.getUserId()));

String retValue = ':All;';
Integer iCount = 0;

for (User usr: [select Name from User where Id in :userlist and isActive = true and Employee_Group__c like '%Sales%' order by Name]) {
retValue += usr.Name + ':' + usr.Name;

iCount++;

if (iCount < userlist.size()) {
retValue += ';';
}
}

return retValue;
}

 

How can I display the retValue, which is the list of users, in a SelectList on my VFPage?  I imagine this is a fairly easy answer, but I just got handed a project and I've just started learning VF.  Thanks for your help!

souvik9086souvik9086

 List<SelectOption> listOptions = new List<SelectOption>();
 listOptions.add(new SelectOption('','--None--'));

List<String> strList = new List<String>();

strList = retValue.split(';');

for(String str : strList){

listOptions.add(new SelectOption(str,str));

}

 

VF Page

 

<apex:selectList id="selected_list" size="1" style="width:250px">
<apex:selectOptions value="{!listOptions}">
</apex:selectOptions>
</apex:selectList> 

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

RiflemanRifleman
Where would I put:
List<SelectOption> listOptions = new List<SelectOption>();
listOptions.add(new SelectOption('','--None--'));
List<String> strList = new List<String>();
strList = retValue.split(';');
for(String str : strList){
listOptions.add(new SelectOption(str,str));
}

And how would I call getUsers() to actually get the retValue?
souvik9086souvik9086

You dont need to call getUsers(). Make retValue as a public variable and add the above code after retValue is populated.

 

public String retValue = ':All;';

public string getUsers() {

 

Set<Id> userlist = new Set<Id>();
userlist.add(UserInfo.getUserId());
userlist.addAll(RoleUtils.getRoleSubordinateUsers(UserInfo.getUserId()));
Integer iCount = 0;
for (User usr: [select Name from User where Id in :userlist and isActive = true and Employee_Group__c like '%Sales%' order by Name]) {
retValue += usr.Name + ':' + usr.Name;
iCount++;
if (iCount < userlist.size()) {
retValue += ';';
}
}
return retValue;
}

 

public List<SelectOption> getOptions(){

List<SelectOption> listOptions = new List<SelectOption>();
List<String> strList = new List<String>();
strList = retValue.split(';');
for(String str : strList){
listOptions.add(new SelectOption(str,str));

 

return listOptions;

}

 

VF Page

 

<apex:selectList id="selected_list" size="1" style="width:250px">
<apex:selectOptions value="{!Options}">
</apex:selectOptions>
</apex:selectList> 

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks