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
Matt EckMatt Eck 

Passing dropdown value to apex class

I'm having what I think is a very simple problem but I can't seem to figure out how to solve it. I'm trying to selsect a feild selected from a dropdown box and pass it to an apex class.

I have this apex class:
public class AssignBadgeToUser {
    public WorkBadgeDefinition curRecord {get; set;}
    public WorkBadge newAssignment {get; set;}
    public WorkThanks thanks{get; set;}
    public List<User> UserTemp = new List<User>();
   
    
    public AssignBadgeToUser(ApexPages.StandardController stdController)
    {
        this.curRecord = (WorkBadgeDefinition)stdController.getRecord();
        this.newAssignment = new WorkBadge();
        
    }
    
    //creates the List to populate the list of users on the visualforce page
    public List<SelectOption> UserList
    {
        get
        {
            UserTemp = [Select LastName, Id, FirstName, Email From User];
            UserList = new List<SelectOption>();
            
            for(User temp : UserTemp)
            {
                UserList.add(new SelectOption(temp.Id, temp.FirstName + ' ' + temp.LastName + ' ' + temp.Id));
            }
            return UserList;
        }
        set;
    }
    
    public void CreateAssignment()
    {
        //WorkThanks record is used to create the thanks.id which is needed as the newassignmanet sourceid
        this.thanks = new WorkThanks(GiverID = UserInfo.getUserId(), Message = 'Automated Message');
        try
        {
            insert this.thanks;
       	} catch(Exception e)
        {
         	System.debug(LoggingLevel.Error, 'Erorr = '+e.getMessage());
        }
        
        //creation of new badge assignment
        this.newAssignment.SourceId = thanks.Id;
        this.newAssignment.RecipientId = '00536000002UKV1'; //Person choosen from the UserList Picklist
        this.newAssignment.DefinitionId = curRecord.Id;
        
        try
        {
            insert this.newAssignment;
       	} catch(Exception e)
        {
         	System.debug(LoggingLevel.Error, 'Erorr = '+e.getMessage());
        }
        
        system.debug(this.newAssignment.SourceId + ' ' + this.newAssignment.RecipientId + ' ' + this.newAssignment.DefinitionId);
    }
}

and this visualforce page:
<apex:page standardController="WorkBadgeDefinition" extensions="AssignBadgeToUser">
   <h1>Assign Badge to User</h1>
   <br/>
  <apex:form >
      <apex:selectList size="1">
          <b>User: </b><apex:selectOptions value="{!UserList}"></apex:selectOptions>
      </apex:selectList>
      <apex:commandButton action="{!CreateAssignment}" value="Submit" id="submitButton"/>
  </apex:form>
</apex:page>

 The dropdown values are generated from UserList on the apex page and displayed on the visualforce page using a selectlist. I want to be able to take one of these choosen by the user and load that value into the create assignment method on line 46 where it says:
this.newAssignment.RecipientId = '00536000002UKV1'; //Person choosen from the UserList Picklist

I'm stuck here and I feel like this should be really simple. any help would be really appreciated.
Marcilio SouzaMarcilio Souza
Hi,

Try to use in SelectOption another value
<apex:selectList size="1"> <b>User: </b><apex:selectOptions value="{!UserChoose}"></apex:selectOptions> </apex:selectList>

public User UserChoose{get; set;} (I guess)

public String UserChoose{get; set;}

UserChoose get the value select in the SelectOptions.


Marcilio