Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (5) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Using sObject Constructors to Save Script Statements

Using sObject Constructors to Save Script Statements

Post by Ankit Arora (forceguru)  (2011-05-05)

Status: Unverified
Level: novice


You want to save script statements when initialising sObjects, perhaps because your object initialising has to take place in a loop.

Problem Description

While this problem may not occur too frequently, you may need to save script statements when creating sObjects. Here's one way of creating an sObject where we create an Account, set values, and then insert it:

Account accObj = new Account();
accObj.Name = 'Test' ;
accObj.Fax = '123445657' ;
accObj.Phone = '123456789' ;
insert accObj ;

Creating the Account sObject consumes 4 statements. If you need to fill in more fields before persisting the sObject, you will of course use more statements.


Instead of creating the sObject instance and then setting each field individually, you can use the sObject constructors to pass in the field values:

Account accObj = new Account(Name = 'Test' , Fax = '12345657' , Phone = '123456789') ;
insert accObj ;

Initialising this sObject only uses 1 script statement. This will obviously play a bigger role if you are forced to create many objects in a loop, and have many fields to initialise.



Recipe Activity - Please Log in to write a comment

If the object in question is an object of memory, example:

Memory object class
public with sharing class MyMemoryObject{
     public String Name{get; set; }
     public String Status{get; set; }

trigger MyTrigger on Opportunity(After insert){

    for( Opportunity opp : ){
        MyMemoryObject obj = new MyMemoryObject();
        obj.Name = 'Texto';
        obj.Status = 'Texto';

How can I  reduce this statement? 


by Carlos Carvalho  (2013-05-16)

Nice practice but often ignored.

voted as verified by Shashikant Sharma  (2011-05-27)

Nice and helpful post.

voted as verified by DevendraNatani  (2011-05-16)

Using a constructor allows you to specifiy the record Id as well, as a result you may not have to query a record to perform an update

Account toUpdate = new Account(
Id = floorRunner.AccountId,
FloorRunner__c = floorRunner.Id

update toUpdate;

by S W  (2011-05-08)

This recepie was really helpful. I was able to reduce the number of script statements while working with hundreds of records.

by smita pandey  (2011-05-06)


Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.