Wednesday 28 October 2020

OAF: How to default current date time into a MessageDateFieldBean in Controller

This post is to show how we can default the current date time to a MessageDateFieldBean in OA framework page, where the field is not mapped to a View Object. If the field is mapped to a ViewAttribute in a View Object, we could just set the value to the ViewAttribute and it will get defaulted in the bean, when the page loads.

We need to make sure that the format matches the user's date preference set as well.

We could use OANLSServices class to acheive this.

Get the current date using java.util.Date. Pass that as an input to OANLSServices.dateTimeToString. This will return the date value in the current user preference format.

Sample Code which can be used in the processRequest function.

 //get the handle to the date field bean  
 OAMessageDateFieldBean dateFieldBean = (OAMessageDateFieldBean)webBean.findChildRecursive("CurrentDate");  
 pageContext.writeDiagnostics(this, "dateFieldBean :" + dateFieldBean, 4);  
 if (dateFieldBean != null)  
 {  
       // Get the current date  
       java.util.Date utilDate = new java.util.Date();  
       //Convert the date value into users date format  
       String dateStr = pageCtxt.getOANLSServices().dateTimeToString(utilDate);  
       pageContext.writeDiagnostics(this, "dateStr :" + dateStr, 4);  
       //set the value to the datefield bean  
       dateFieldBean.setValue(pageContext,dateStr);  
 }  

The above code will take care of the date format set in the user preferences.

Date Format set to dd-MMM-yyyy

Output
Date Format set to yyyy/MMM/dd


Output




Feel free to point out if anything is missing/wrong in this blog.