Follow by Email

Saturday, 11 June 2016

ADF Basics: Use transient attribute to show description of selected value in case of input and combo Lov


In ADF often We apply LOV on a ID attribute to show it's Description to user, This works good in case of choice list (selectOneChoice) but if we use Input Text with List of Values (<af:inputListOfValues>) or Combo Box with List of Values (<af:inputComboboxListOfValues>) then after selection ID appears on page instead of  Description like this


So to avoid this we apply LOV on a transient attribute that shows Description and sets ID value DB attribute, See how to implement this


Here I am using Departments and Employees table of HR Schema to prepare Model


Create a transient attribute of String type (to show Description) in Employes viewObject to apply LOV on that,


 Set updatable Always for transient attribute


Now apply LOV on this attribute using Departments viewObject and pass DepartmentId to ID DB attribute of Employees ViewObject using List Return Values


On UI Hints select DepartmentName as we want to show this to user


Now drop Employees viewObject as form on page and see that LOV is working properly except DepartmentName is not appearing though it's Id is saved in DB


To populate DepartmentName of respectived DepartmentId in transient attribute we can use these two methods

1. Using findByKey() in Groovy Expression


We can use Groovy Expression to populate transient attribute value based of Key attribute of Lov ViewObject. Here Department Id is primary key of Departments ViewObject so we can use Lov Accessor to find DepartmentName using findByKey method


I have used this expression, here I made a JBO Key using key attribute DepartmentId (Departments ViewObject) , DepartmentsVO1 is the name Lov ViewAccessor

if(DepartmentId!=null){
oracle.jbo.Key keyVal=new oracle.jbo.Key(DepartmentId);
return DepartmentsVO1.findByKey(keyVal,1)[0].getAttribute("DepartmentName");
}
else{
return null;
}

Now go to viewObject source and change expression trust mode to trusted



All Done :) Run and check application, LOV Description is appearing

2. Using Java Code in ViewObject RowImpl Class 


We can write logic to get LOV Desciption in transient's getter method in RowImpl Class of Employees viewObject
First step is to create RowImpl class for Employees ViewObject


Go to RowImpl class and write this code in getter method of transient attribute, this code filters Lov accessor using repective DepartmentId and retrieves DepartmentName

    /**
     * Gets the attribute value for the calculated attribute DeptNameTrans.
     * @return the DeptNameTrans
     */
    public String getDeptNameTrans() {
        Integer departmentId = null;
        String departmentName = null;
        //Check if DepartmentId is not null then get DepartmentName for that Id
        if (getDepartmentId() != null) {
            departmentId = getDepartmentId();
            //getDepartmentsVO1 is ViewAccessor of LOV ViewObject
            Row[] deptRows;
            deptRows = getDepartmentsVO1().getFilteredRows("DepartmentId", departmentId);
            if (deptRows.length > 0) {
                departmentName = deptRows[0].getAttribute("DepartmentName").toString();
            }
            return departmentName;

        } else {
            return (String) getAttributeInternal(DEPTNAMETRANS);
        }

    }

All Done :)
Sample ADF Application (Jdev 12.1.3)- Download
Cheers :) Happy Learning

7 comments :

  1. Excuse me i get this error The variable [DepartmentsVO1] is Undeclared. My version is 12.2.1. Can you help me resolve this ?

    ReplyDelete
    Replies
    1. Huy

      Check that DepartmentsVo1 is added as viewAccessor in Employees VO or not ?
      Use Lov Accessor name in expression and use findByKey on that

      Ashish

      Delete
  2. you have to check the dependency of departmentID in the transient Attribute

    ReplyDelete
  3. Hello

    I just went through your blog. This works when the value has been selected .
    I have a scenario when i want input box to be populated at run when user selects a value

    Can you help

    Thanks
    Pritee

    ReplyDelete
    Replies
    1. Hi Pritee

      This will work in your case too, just put proper partial triggers on input text

      Ashish

      Delete