I've already set a default value for the field name with testUser

However, when I tried to create an object from Account.

The object still not coming with the default value on the name field.

Any possible to do so?

Because I'm creating a Web API (non-restful) and pass a new object with default values to the front-end.

How could I get it?

Why doesn't Salesforce provide with such an essential function?

Account acc = new Account();

System.Debug(LoggingLevel.DEBUG, JSON.serializePretty(

Expected output

10:57:50.27 (29577741)|USER_DEBUG|[3]|DEBUG|{
  "attributes" : {
    "type" : "Account"
  "Name" : "testUser"

Actual output

10:57:50.27 (29577741)|USER_DEBUG|[3]|DEBUG|{
  "attributes" : {
    "type" : "Account"
  • you should be able to see the Json of account in debug logs but are you saying that you don't see any new account created with that name? – night crawler Jan 18 '17 at 18:56
  • pls see my update, thanks~~ I provide more concrete outputs – newBike Jan 18 '17 at 18:59
  • where did you set the default value for the account? I don't see it now? – night crawler Jan 18 '17 at 19:21
  • I set the value here . i.imgur.com/XUyK3NY.png – newBike Jan 18 '17 at 19:26

You can instantiate a record with its default values using the SObjectType method newSObject. Note it has a signature which accepts recordTypeId and loadDefaults.

SObjectType accountType = Account.sObjectType;
Account recordWithDefaults = (Account)accountType.newSObject(/*recordTypeId*/ null, /*loadDefaults*/ true);
| improve this answer | |
  • I know it's usually not a good thing to change an accepted answer, but I am doing it since the right method is now . newSObject not .newInstance. – Sebastian Kessel Aug 10 '18 at 17:23
  • 1
    @SebastianKessel No problem nice catch – Adrian Larson Aug 10 '18 at 18:31

Default values will be populated for unspecified fields at the moment the record is inserted, not when a JSON deserialize is performed. You can also describe fields and get the getDefaultValue method from the DescribeSObjectFieldResult class.

| improve this answer | |
  • It doesn't work for custom objects. I also have a custom object Loan_Account__c . And I got this exception Variable does not exist: Loan_Account__c.Description – newBike Jan 18 '17 at 19:25
  • the field should be Loan_Account__c.Description__c – night crawler Jan 18 '17 at 19:28
  • Still not working on custome objects. Loan_Account__c.Description__c Compile failure on line 3, column 9: Variable does not exist: Loan_Account__c.Description__c – newBike Jan 18 '17 at 19:45
  • can you please post the whole code? Also, please check field accessibility for that field – night crawler Jan 18 '17 at 19:47
  • @newBike I have to agree. It sounds like a compilation error. You're doing something not quite right. – sfdcfox Jan 18 '17 at 19:47

Finally, I found salesforce provides this feature actually.

Schema.getGlobalDescribe().get('loan_account__c').newSObject(null, true)

Put the first argument as null for the ID and set 2nd arg as true for loading defaults.

| improve this answer | |
  • 2
    So if you compare that with my answer, the difference is that instead SObjectType accountType = Account.sObjectType, you're doing a more verbose (and dramatically less efficient) SObjectType accountType = Schema.getGlobalDescribe().get('loan_account__c'). Getting the global describe consumes a lot of CPU Time, so avoid it if you can. Just change Account.sObjectType to Loan_Account__c.sObjectType. – Adrian Larson Jan 18 '17 at 21:30

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.