I was going through a document at the SObjectType
class where there was a method named - newSObject(recordTypeId, loadDefaults)
. I see that using this method one can create a new Sobject with default values of
custom fields assigned. But I am started to think that what is the difference between below two ways of creating a object say Account object:
Suppose there is a custom field on account named class__c with a default value as "Fresh".
1st approach: this will create a new account- Acme, with a default value of class__c as "Fresh":
Account acc = new Account(Name='Acme);
Insert acc;
2nd approach: this is from the documentation:
// 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;
I am a bit confused here that why would we use Account.sObjectType.newSObject(null, true);
to populate the default value of custom field when the default value of field
gets populated automatically when a new account is created. My question is why do we need to explicitly do Account.sObjectType.newSObject(null, true)
? What is a difference between
1st way and 2nd way. I am sure I am missing some concept here:)