I would like to set a custom number field to default to 0 as the default value. I tried entering 0 on the default formula editor and even tried 1*0 as the default value of the number field. I am always getting a null. How do i make a number field to default to 0? I am stumped Buyan

  • 1
    It works for me...can you post some code that is not working? A concise unit test could be illustrative. – Adrian Larson Mar 14 '16 at 21:06
  • 1
    Works as expected for me as well. How are you creating the record? Apex? Standard Detail page? – Mark Pond Mar 14 '16 at 23:01

One mechanism of creating a new record with the default values in Apex is to use the Schema.SObjectType.newSObject() method and specify that you want the instance created with the defaults (field values & Record Type).

The first parameter is the RecordType Id and the second parameter indicates if default values should be set for its custom fields.

// Create an account with predefined default values
Account acct = (Account)Account.sObjectType.newSObject(null, true);
// Provide a value for Name
acct.Name = 'Acme';
// Insert new account
insert acct;

Documentation: SObjectType Class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.