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
Amoun777Amoun777 

Trying to instantiate a custom object with a default value in a field

I may be missing something simple but I cannot seem to get a default Number value to appear in an object I instantiate in apex.  The value is always == null.  My variable is Mileage_Rate__c and in my org the default value = 0.58.  When I create an instance using the following code this value is always null.  I do not want to hardcode this in my apex class.  Any suggestions? 

 

apex:

 

Expense_Report = new Expense_Report__c(Status__c = 'Unsaved');

...

if(Expense_Report.Mileage_Rate__c == null)

{

   // This code will always execute 

}

 

Thanks in advance for any help on this!

 

Adam

jwetzlerjwetzler
Moving this thread to the Apex Code forums.
hisrinuhisrinu

Hi,

 

 Once you saved the record to the database then only the auto number field will be not null.

 Until and unless you save it to the database it will be always null.