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
The KnightThe Knight 

Describe field result getDefaultValue is returning null

I am trying to fetch the default values for the fields. I am doing

 

Schema.DescribeSObjectResult DSR = myObj__c.getSObjectType().getDescribe(); 
    			Map<String, Schema.SObjectField> CRFieldsMaps = DSR.fields.getMap();
	            for(String keyMap:CRFieldsMaps.keySet()){
	            	Schema.DescribeFieldResult descField = CRFieldsMaps.get(keyMap).getDescribe();

descField.getDefaultValue();

}

descField.getDefaultValue() returns null.

I am having date/time field with NOW() as default value.

 

If I use defaultValueFormula() this returns the formula not the value.

 

 

 

forecast_is_cloudyforecast_is_cloudy

The describe result will only return metadata about the field/object. In this case, the formula ('NOW()') is infact the associated metadata for 'Default Value'. You can't get actual data (which is what the value of 'NOW()' would be) via a describe call. For that you'll have to query an actual record in the DB.

Hope this clarifies somewhat.

The KnightThe Knight

Hi

In the VF component reference it is mentioned like

 

"Beginning with API version 20.0, an inputField matched to a field with a default value has the default value prepopulated on the Visualforce page."

 

Unfortunately this is not happening by using Standard as well as custom controller.Is this a known defect?

This is why I tried to fire a describe call and fetch the default values in the controller and found that it is returning meta data but not the actual data.

 

You mentioned that I need to fire a query to that actual record in DB. I did not get that part . Could you explain me how this is going to help me to fetch the default values and prepopulate?

 

 

 

 

forecast_is_cloudyforecast_is_cloudy

I just tried this and it works for me. I created a test date/time field and set its defaut value to 'NOW()'. When I referenced that field in an inputField field in a VF page (I used the Standard Controller), the current date/time did in fact show up as the default value.

A question for you. Are you viewing an existing record with your VF page (i.e. a 'view/edit/delete' scenario) or are you using the VF page to create a new record (i.e. a 'new' scenario). If former, then the default value for the field will NOT be displayed via the inputField tag (since its bound to an actual record in the DB). If latter, then you should get the correct default value displayed in the page. Hope this makes sense.

The KnightThe Knight

Hi forecast_is_cloudy, 

 

When I used {get;set;} it worked fine. But if I use conventional getter setter it is not working.

 

Page:

 

<apex:page controller="MyobjController">
   <apex:form >
  <apex:pageBlock >
  <apex:pageblocksection >
  <apex:inputfield value="{!myobj.Date_time_Field__c}"/>
  <apex:inputfield value="{!myobj.number_Field__c}"/>
  
  </apex:pageblocksection>
</apex:pageBlock>
<apex:commandButton action="{!save}" value="save"/>
  </apex:form>
  </apex:page>

 

 

Controller:

 

public class MyobjController{
public MyObj__c myobj = new MyObj__c();
public MyObj__c getmyobj(){
  return myobj;
}
public void setmyobj(MyObj__c myobj ){
 this.myobj = myobj;
}
public PageReference save(){
 return null;
}

}

 

If I replace the above getter setters with {get;set;} it was working fine. Please let me know the difference.