function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
marcobmarcob 

what is the best apex code?

Hi,

what is better:

 

string querystring = 'select id, field_a, field_b from <custom object> where id =: <variable>';

 

Alternative 1:

for(<custom object> co :database.query(selectie)){

   myList.add(co);

}

 

Alternative 2:

myList = database.query(selectie);

 

Any suggestions?

 

Thx in advance!

Marco

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SamuelDeRyckeSamuelDeRycke

Definitely 2, why waste resources iterating a result list, to add it's elements to another list when you can assign the list immediately. Neither cases need exception handeling if you ask me. Coding efficiently is not always about script statements, sometimes more code, can equal a higher efficiency, but not in this case.

 

 

string querystring = 'select id, name from testobject__c' ;
long start = system.currentTimeMillis();
list<testobject__c>mylist = new list<testobject__c>();
for(testobject__C to :database.query(querystring)){
   myList.add(to);
}
long msSpan = system.currentTimeMillis()- start;
system.debug('----------------------------->list size:'+mylist.size());
system.debug('----------------------------->exc ms span:'+ msSpan);

//output:
09:51:46.285 (285475000)|USER_DEBUG|[12]|DEBUG|----------------------------->list size:515
09:51:46.285 (285609000)|USER_DEBUG|[13]|DEBUG|----------------------------->exc ms span:240

 

string querystring = 'select id, name from testobject__c' ;
long start = system.currentTimeMillis();
list<testobject__c>mylist = database.query(querystring);
long msSpan = system.currentTimeMillis()- start;
system.debug('----------------------------->list size:'+mylist.size());
system.debug('----------------------------->exc ms span:'+ msSpan);

//output:
09:53:50.071 (71817000)|USER_DEBUG|[9]|DEBUG|----------------------------->list size:515
09:53:50.071 (71918000)|USER_DEBUG|[10]|DEBUG|----------------------------->exc ms span:25

 

All Answers

Naidu PothiniNaidu Pothini
string querystring = 'SELECT Id, Name FROM Account LIMIT 10';

List<Account> myList = new List<Account>();

for(Account co :database.query(querystring ))
{
   myList.add(co);
}
 
Total Number of Script Statements :10

 

 

string querystring = 'SELECT Id, Name FROM Account LIMIT 10';

List<Account> myList = new List<Account>(database.Query(queryString));


 
Total Number of Script Statements :1

 

marcobmarcob

And the one that results in 1 script statement doesn't require additional error handling, like when the number of rows found = zero?

SamuelDeRyckeSamuelDeRycke

Definitely 2, why waste resources iterating a result list, to add it's elements to another list when you can assign the list immediately. Neither cases need exception handeling if you ask me. Coding efficiently is not always about script statements, sometimes more code, can equal a higher efficiency, but not in this case.

 

 

string querystring = 'select id, name from testobject__c' ;
long start = system.currentTimeMillis();
list<testobject__c>mylist = new list<testobject__c>();
for(testobject__C to :database.query(querystring)){
   myList.add(to);
}
long msSpan = system.currentTimeMillis()- start;
system.debug('----------------------------->list size:'+mylist.size());
system.debug('----------------------------->exc ms span:'+ msSpan);

//output:
09:51:46.285 (285475000)|USER_DEBUG|[12]|DEBUG|----------------------------->list size:515
09:51:46.285 (285609000)|USER_DEBUG|[13]|DEBUG|----------------------------->exc ms span:240

 

string querystring = 'select id, name from testobject__c' ;
long start = system.currentTimeMillis();
list<testobject__c>mylist = database.query(querystring);
long msSpan = system.currentTimeMillis()- start;
system.debug('----------------------------->list size:'+mylist.size());
system.debug('----------------------------->exc ms span:'+ msSpan);

//output:
09:53:50.071 (71817000)|USER_DEBUG|[9]|DEBUG|----------------------------->list size:515
09:53:50.071 (71918000)|USER_DEBUG|[10]|DEBUG|----------------------------->exc ms span:25

 

This was selected as the best answer
marcobmarcob

Well, this leaves no doubt, 2 is the winner!

 

Thanks for your help!

 

Br, Marco.