Monday, January 19, 2015

Get SelectOneChoice Code/Values in Managed Bean - ADF

The repeated question on OTN is how to get the code or value  of the selected item from SelectOneChoice. One of my post explains how we can achieve this using variable iterators in page definition. Here is the link for the post : http://rohanwalia.blogspot.sg/2013/09/retrieve-selected-value-from.html

Using the technique described in the above mentioned approach, we get hold of the id or value when we select the item from select one choice and we can use it on the page.

But the problem comes when this value is needed in a method of managed bean using ValueChange Listener . One scenario for this is that the code of selected item is required as an input to call a method exposed by ApplicationModule.

So our use case will be to get EmployeeId of selected Employee from LOV into valueChangeListener.

To start with lets quickly create a sample application. Used HR schema and created one jspx page selectOneChoice.jspx .

Go to the binding and create a new variable inside variable iterator.




So after creating the variable of type java.lang.String this is how it looks in structure window.





Now, we need to create a new list binding.  Go to bindings and add new list binding.




Select the option as above and then next will be the binding screen. Base Data Source is variable and List Data Source is EmployeeVO.





We have selected to display attribute as First Name of the employee. Ok so we are done list binding.
Value of selected List Attribute (EmployeeId) will be saved (mapped) in employeeId as done in Data Mapping.

Now create a attribute value binding with data source pointing to the variable employeeId in variable iterator.



At the end bindings will be like below :


employeeId1 is list binding and employee2 is attribute value both pointing to variable iterator.

Now lets select a select one choice from component palette.  Modify the code of select one choice as below.




Select One Choice value as well as selectedItems point to list binding(employeeId1). Added an output text which refers to attribute value which we created in bindings.  This is the thing which will give us the selected value.

If page run now, it gives the desired output.



But the problem comes when we need this value in our valueChangeListner. Lets add valueChangeListener and create a method in managed bean. Now if we evaluate this, the expression
#{bindings.employeeId2.inputValue} evaluates to null.


Here is the expression in the bean. For the first time , the employeeId comes as Null.


But on the UI the outputFormatted is showing correct value i.e. 103


Hmmm, this is because the valueChangeListner gets executed in processValidation phase and the value of Select One Choice is not updated to model. This is the reason value in ValueChange Listener is null and after it executes, model is updated and then it is evaluated.

So we need to do something to update model of SelectOneChoice while we are in valueChangeListener.


Ok So we will add the below code in valueChangeListener.




The two lines of code added actually updates the mode for SOC.

Now as you can see we get the value in Managed Bean Method !!


By this way getting any value from the row of selected value from LOV and get it into bean method should be easy :)

That's it !!

Happy Learning 







8 comments:

  1. hello
    how can i send you mail for some question to help me

    ReplyDelete
  2. Hi Subhi,
    You can anytime send your questions to er.walia@gmail.com and also you can mention myself on twitter : @rohanwalia17

    Thanks

    ReplyDelete
  3. thanks,mr Rohan Walia
    what about getting value of selectoneChoice in valuechanagelistener by :
    public void SOCI_valueChangeListener(ValueChangeEvent vce)
    {
    String val=()vce.getNewValue().toString;
    System.out.printLn(s);
    }

    ReplyDelete
  4. Nice Article, It saved time. Thanks Rohan Bhai !!

    ReplyDelete
  5. Thanks Rajesh !!

    Visiterman, you will only get the index by default. That''s why this approach is used.

    Thanks
    Rohan

    ReplyDelete
  6. Your most welcome Mauriel.

    Thanks
    Rohan

    ReplyDelete