+ Start a Discussion

sObjects topic on Trailhead

On https://developer.salesforce.com/trailhead/apex_database/apex_database_sobjects, it gives 2 examples on how to populate fields on a new record: is either one better than the other, in terms of taking up memory, or any governance limits? thanks.

1) Account acct = new Account(Name='Acme', Phone='(415)555-1212', NumberOfEmployees=100); OR
2) Alternatively, you can use the dot notation to add fields to an sObject. The following is equivalent to the previous example, although it takes a few more lines of code.view source
 Account acct = new Account();
 acct.Name = 'Acme';
 acct.Phone = '(415)555-1212';
 acct.NumberOfEmployees = 100;
One is not better than the other and since APEX mesaures character count, they are the same as far as your APEX code limit. Most I know prefer the second version as it provides a "cleaner" code review for others to see your code (especially when inserting values into a new SObject with variables)

Both are better, as earlier salesforce have script statment limits, at that time first was preferable but now salesforce has removed this limit, so you can go ahead with any approach.

Also, you can use variables in both the approaches.

If this help, you can mark it as best answer.