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
BaguiarBaguiar 

VF apex:SelectList value to Controller

Hi There,

 

I'm trying to get the value from a Selectlist in my VF page to my controller "Task_Leadership". The VF page works fine and the "refresh" button works good too. BUt, I can;t get the value from the SelectList with the ID = "Positions" to my controller. I've tried " ApexPages.currentPage().getParameters().get('Positions');" and no success. Here is the Page and Class:

 

 

<apex:page controller="Taskleader" showHeader="false" >
<apex:form >
<apex:pageBlock id="pageBlock" Title="Activities logged with Presidents in the Last 90 days">
<apex:pageMessages ></apex:pageMessages>
<apex:selectList id="Positions" size="1">
<apex:selectoption itemLabel="President" itemValue="President"></apex:selectoption>
<apex:selectoption itemLabel="Co-President" itemValue="Co-President"></apex:selectoption>
<apex:selectoption itemLabel="Education director" itemValue="Education director"></apex:selectoption>
</apex:selectList>
<apex:commandButton action="{!Refreshtsk}" value="Run Report" />
<apex:pageBlockTable value="{!tsk}" var="tskl" rendered="{!NOT(ISNULL(tsk))}">
<apex:column headerValue="Name" ><apex:outputLink value="/{!tskl.who.id}" target="_parent">{!tskl.who.Name}
</apex:outputLink></apex:column>
<apex:column headerValue="Subject" ><apex:outputLink value="/{!tskl.id}" target="_parent">{!tskl.subject}
</apex:outputLink></apex:column>
<apex:column value="{!tskl.Activity_Type__c}"></apex:column>
<apex:column value="{!tskl.status}"></apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>

 

public class Taskleader
{

    public PageReference Refreshtsk() {
        return null;
    }

  String POS = ApexPages.currentPage().getParameters().get('Positions');
  private List<task> tsk;
  public List<task> gettsk()

    {
    TAsk[] tsk = [select id, subject, who.name, status, Activity_type__C, activitydate, what.name, Account.BillingCity, Account.BillingState FROM Task WHERE WhoID IN ( SELECT Contact__c FROM Leadership_position_code__c WHERE Position__c = :POS and  (Thru_Date__c = null OR Thru_Date__c > :system.today()) and (Start_Date__C <= :system.today()) ) and OwnerID = :UserInfo.getUserId() and createddate >= :system.today()-90];
    return tsk;
    }
}

 

 

Thanks as usual!!

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You'd get the value to the controller in much the same way as with any other input type - using a controller property to back the input.

 

Something like:

 

Controller

 

 

public String positions {get; set;}

 

 

and on the page:

 

 

<apex:selectList id="Positions" value="{!positions}" size="1">

 

 

That way, when the form is submitted, the positions property will be updated with the user's choice.

 

If you are allowing multiple selection, the positions property would need to be an array of strings.

 

All Answers

bob_buzzardbob_buzzard

You'd get the value to the controller in much the same way as with any other input type - using a controller property to back the input.

 

Something like:

 

Controller

 

 

public String positions {get; set;}

 

 

and on the page:

 

 

<apex:selectList id="Positions" value="{!positions}" size="1">

 

 

That way, when the form is submitted, the positions property will be updated with the user's choice.

 

If you are allowing multiple selection, the positions property would need to be an array of strings.

 

This was selected as the best answer
BaguiarBaguiar

Thanks Bob! that worked like a champ.

But can you explain me why the need of       value="{!positions}"     in the selectlist ?  I thought that would be a formula to list values on the list, without manually creating them on the selectoptions ....

 

 

bob_buzzardbob_buzzard

This is the selected value for the list - the selectoptions can be generated in one go from the controller if necessary.

 

The value="{!property}" is the standard VF pattern for associating a value in the controller with an input component on the screen.

BaguiarBaguiar

Great! got it.

 

one more thing using the same example, I'm now trying to calculate the date range using the same solution.

Created a page:

 

 

<apex:selectList id="rangedate" value="{!rangedate}" size="1" title="Last X Days">
<apex:selectoption itemLabel="30" itemValue="30"></apex:selectoption>
<apex:selectoption itemLabel="60" itemValue="60"></apex:selectoption>
<apex:selectoption itemLabel="90" itemValue="90"></apex:selectoption>
</apex:selectList>

 

 

and the class:

 

public String positions {get; set;}
 public String rangedate {get; set;}
  private List<task> tsk;
  public List<task> gettsk()

    {
    TAsk[] tsk = [select id, subject, who.name FROM Task WHERE WhoID IN ( SELECT Contact__c FROM Leadership_position_code__c WHERE Position__c = :positions and  createddate >= :system.today()- :rangedate];
    return tsk;

 

 

But seems that i can't calculate the Date range using a string ":rangedate". is there any other way to get the values to calculate the Createddate - Rangedate ?

 

Thanks again!

bob_buzzardbob_buzzard

Yes, I can't say I'm surprised it doesn't like the arithmetic in the SOQL statement.

 

I'd do it by creating a local variable that is a date.  E.g.

 

 

 Date candDate=System.today();
 candDate=candDate+Integer.valueOf(rangeDate);

 TAsk[] tsk = [select id, subject, who.name FROM Task WHERE WhoID IN ( SELECT Contact__c FROM Leadership_position_code__c WHERE Position__c = :positions and  createddate >= :candDate];

 

 

BaguiarBaguiar

Cool! thanks a bunch for the help. having a little issue with the inital load of the page with a null value dor the "rangedate" but working on it..

 

THANKS A MILLION!

BaguiarBaguiar

Hi bob,

 

I've been fighting this thing for a while due to the fact that when the VF page is loaded, the value for the "ragedate" is Null. Than I get an error as  the VF page tells me that the Argument in Integer.valueOf(rangeDate)  can't be null.

 

OK, I've tried an If statement :

 

public String rangedate {
  get {
            if (rangedate == null) rangedate = '30' ;
            return rangedate;
        }
        set;
    }

  Date candDate=System.today();
  Date candDate1=candDate-Integer.valueOf(rangeDate);

 Which lets me load the page, but regardless of the value I select on the "rangedate" list on the VF page and hit the "refresh" button, It keeps going default to 30  (as I beleive it is beacuse it still as "null")

 

Also tried:

 

public String positions {get; set;}
  public String rangedate {get; set;}
           
    Date candDate=System.today();
    Date candDate1=candDate-(rangedate== null ? 400 : Integer.valueOf(rangeDate));

 

 

Thinking that it would work just as the "positions" list selection but no Luck.

 

What's weird is that the "positions" list selection works just fine, as a make a diferent selection, hit the "refresh" button and the records are updated fine. No success on the "rangedate" though.  Thanks again for the great help as usual.

B

 

 

 

bob_buzzardbob_buzzard

Is 30 the default value for rangedate?

 

If so, the easiest thing to do is initialise the value in the constructor of the controller.

 

E.g.

 

 

public TaskLeader()
{
   rangeDate='30';
}

 

 

When the page is first rendered, this should automatically choose the 30 value from the selectlist.  After that, the property should pick up what the user has selected through standard VF behaviour.

 

You should just be able to leave the rangeDate declaration as:

 

 

public String rangeDate {get; set;}

 

 

BaguiarBaguiar

Bob, did that but the page still seems to render initially with a null value for date range. When I load the page I get the exact error:

 

 System.NullPointerException: Argument 1 cannot be null

Class.Taskleader: line 15, column 45 External entry point

 

 Line 15, column 45 is the interger value of "rangedate". here is the controller:

 

public class Taskleader
{ 
      public PageReference Refreshtsk() {
            return page.task_leadership;
         }
      public TaskLeader()
         {
         rangedate='30';
         }

  public String positions {get; set;}
  public String rangedate {get; set;}

    Date candDate=System.today();
    Date candDate1=candDate-Integer.valueOf(rangedate);

  private List<task> tsk;
  public List<task> gettsk()

    {
    TAsk[] tsk = [select id, subject, who.name, status, Activity_type__C, activitydate, what.name, Account.BillingCity, Account.BillingState FROM Task WHERE WhoID IN ( SELECT Contact__c FROM Leadership_position_code__c WHERE Position__c = :positions and  (Thru_Date__c = null OR Thru_Date__c > :system.today()) and (Start_Date__C <= :system.today()) ) and OwnerID = :UserInfo.getUserId() and createddate >= :candDate1];
    return tsk;
    }
}

 I'll post the VF again just in case.

 

<apex:page controller="Taskleader" showHeader="false" >
<apex:form >
<apex:pageBlock id="pageBlock" Title="Activities logged with Key Leadership">
<apex:pageMessages ></apex:pageMessages>
<b>Select Position:   </b>
<apex:selectList id="Positions" value="{!positions}" size="1">
<apex:selectoption itemLabel="President" itemValue="President"></apex:selectoption>
<apex:selectoption itemLabel="Co-President" itemValue="Co-President"></apex:selectoption>
</apex:selectList>
<b> How many days back? </b>
<apex:selectList id="rangedate" value="{!rangedate}" size="1" >
<apex:selectoption itemLabel="30" itemValue="30"></apex:selectoption>
<apex:selectoption itemLabel="60" itemValue="60"></apex:selectoption>
</apex:selectList>
<b>   </b>
<apex:commandButton action="{!Refreshtsk}" value="Run Report" />
<apex:pageBlockTable value="{!tsk}" var="tskl" rendered="{!NOT(ISNULL(tsk))}">
<apex:column headerValue="Name" ><apex:outputLink value="/{!tskl.who.id}" target="_parent">{!tskl.who.Name}

</apex:outputLink></apex:column>
<apex:column headerValue="Subject" ><apex:outputLink value="/{!tskl.id}" target="_parent">{!tskl.subject}

</apex:outputLink></apex:column>
<apex:column value="{!tskl.Activity_Type__c}"></apex:column>
<apex:column value="{!tskl.status}"></apex:column>
<apex:column value="{!tskl.activitydate}"></apex:column>
<apex:column headerValue="Congregation" value="{!tskl.what.name}"></apex:column>
<apex:column value="{!tskl.account.billingcity}"></apex:column>
<apex:column value="{!tskl.account.billingState}"></apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>

</apex:page>

 

Again, your help is GREAT!

B

bob_buzzardbob_buzzard

I'd expect that given the following lines of code:

 

 

 public String positions {get; set;}
  public String rangedate {get; set;}

    Date candDate=System.today();
    Date candDate1=candDate-Integer.valueOf(rangedate);

 range date is declared but not initialized, so will have the default value of null.  candDate1 then uses rangeDate a couple of lines later, when it is still null.  

 

If you initialise rangeDate in the constructor also you should have more luck.  E.g.

 

 

  public String positions {get; set;}
  public String rangedate {get; set;}

  Date candDate;
  Date candDate1;

  public TaskLeader()
  {
     rangedate='30';
     candDate=System.today();
     candDate1=candDate-Integer.valueOf(rangedate);
  }

 

 

 

 

 

BaguiarBaguiar

Hi Bob,

 

really got what you mean on initialising the rangedate in the constructor. The only thing is that now, regardless of what I select on the rangedate SelectList, the value is allways 30. I thought this would only default the initial value for rendering when the VF is loaded. NO matter what I select now, the report is going back only for 30 days...

 

Thanks Bob, this is driving me crazy...

bob_buzzardbob_buzzard

We'll get there.

 

The reason for this is that candDate1 is initialised in the constructor but never changed.  

 

You'll need something like:

 

 

  public List<task> gettsk()

    {
     candDate1=candDate-Integer.valueOf(rangedate);
    TAsk[] tsk = [select id, subject, who.name, status, Activity_type__C, activitydate, what.name, Account.BillingCity, Account.BillingState FROM Task WHERE WhoID IN ( SELECT Contact__c FROM Leadership_position_code__c WHERE Position__c = :POS and  (Thru_Date__c = null OR Thru_Date__c > :system.today()) and (Start_Date__C <= :system.today()) ) and OwnerID = :UserInfo.getUserId() and createddate >= :system.today()-90];
    return tsk;
    }

 

 

In this case, candDate1 reflects the latest value of rangedate to be submitted back.

BaguiarBaguiar

That did it!

 

Great. The Canddate1 needs to be on the public list <task> as it is what is being reloaded on the page and not the constructor. I really thank you for that Bob. Learning a lot with these little headaches.. :)

 

B    

 

TannyTanny

I have a standard controller and a select list(Multiselect=true) on the visual force page. I want to store these values into a field, say

<apex:selectList id="minorityCategory" multiselect="true" size="3" value="{!Application__c.Minority_Category1__c}">
<apex:selectOptions value="{!minorityCategories}"/>
</apex:selectList>

 

The values coming from the controller are seaparated by comma and I need to replace it with semi colon.

So is there any way I can manipulate the string coming from the controller and then save to the field. Because as far as my knoledge goes, the set functionality is happening automatically and I do not have control on it.

 

Controller code:

public List<SelectOption> getMinorityCategories() {
List<SelectOption> options = new List<SelectOption>();
//options.add(new SelectOption('-- None --', ''));

Schema.DescribeFieldResult fieldResult =
Account.Minority_Category1__c.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

for( Schema.PicklistEntry f : ple) {
options.add(new SelectOption(f.getLabel(), f.getValue()));
}
return options;
}