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
nitin sharma 190nitin sharma 190 

About Soql queries and Governor limits

Hi All,

I am trying  some experments  in salesforce dev org.I cannot do things which require more data due to restrictions by salesforce.However,I am sure I can get help from you guys.

As per governor limit chart.

Total number of records retrieved by SOQL queries is 50,000

As per governor limts I can retrieve 50,000 records by doing a single query.Now my query is given below.

List<Account> aa = [SELECT Id, Name FROM Account WHERE Name = 'Acme'];

1)If my query retrieves moe than 50,000 records and assign them to the list then governor limit will be in place and It will give an error.Am I right?If the records are equal or less than 50,000 then governore limits will not have any impact .Please correct me if I am wrong

SOQL For loop
for (variable_list : [soql_query]) {
   code_block
}

What will happen in the above given case.if my query in the for loop fetch more than 50,000  then it should hit governor limits and give me an error.Is that correct?.However,if the total number of records retrieved by th query is less than 50,000 then it will not give an erro message of "Total number of records retrieved by SOQL queries 50,000"?Is that correct.

However, I will be having  dml update statement within the for loop  and records are more than 20,000 then I will hit dml limit of 150 per apex transaction because per execution of loop can only process 200 records.In that scenario I cannot use that loop and I need to use use batch apex to update records in the system.Is that Correct?

I am trying to understand above mentioned things from the perpective of salesforce certification So somebody please help and clear my doubts

And do tell me how many maximum records I can reterieve by using a single query.I think i can retrieve 50,000 records by using single query anbywhere in the system be inline soql for loop or single query in a salesfore classs method.Please help



 
Shukla YogeshShukla Yogesh

Hi Nitin,

The number of records retrieved by SOQL depends upon they are fetched from database.
For example using a simple SOQL statement limit is 50000 but using Database.QueryLocator limit is 10000 and for a batch apex limit is 50million and same way @ReadOnly annotation in web services give you access to unrestricted records but given the condition of no DML.

Regarding your query to DML limit of 150 in for loop... We don't put dml in for loop. You can manipulate the data in collection and after the loop write the DML statement which is just 1. :)

 

Please let me know in case of any doubt and mark the answer best if it helped you!

BR,
Yogesh

nitin sharma 190nitin sharma 190
Hi Yogesh,

Please read the below given lines:- 

The sObject list format executes the for loop's <code_block> once per list of 200 sObjects. Consequently, it is a little more difficult to understand and use, but is the optimal choice if you need to use DML statements within the for loop body. Each DML statement can bulk process a list of sObjects at a time.


SOQL For loop
for (variable_list : [soql_query]) {
   code_block
}



If we dont put dml in the above goven for loop then how do we updaate 200 records at once and then another set of 200 records.As fas as iI know the above given loop will process 200 records at once and then it will process another 200 set of records and so and so forth.In order to do that we need to  have dml statement.



Secondly,How many records soql query in the below given loop will fetch .Can it fetch more than 50,000 records or no?

for (variable_list : [soql_query]) {
   code_block
}


Thank Nitin

 
Shukla YogeshShukla Yogesh

Hi Nitin,

 

Inside for loop you can create a collection of records and add the records for each iteration and after the loop you can just write dml statement which will count 1 governor limit.

We can't fetch more that 50K records to do so we need asynchronous batch job which can operate on 50 Million records.

 

I hope it helps. Please visit best practices here:
https://developer.salesforce.com/page/Apex_Code_Best_Practices

Please meak the answer as best if it solved your query.

BR,
Yogesh

nitin sharma 190nitin sharma 190
Hi yogesh,
Thanks for the link and your explanation .Now please look at the given code 
list<account> acc=new list<account>();
static integer i=0;
for(list<account>s:[select name from account limit 400 ])
{
    for(account  up:s)
    {
        
    up.name='india';
    acc.add(up);
        i=i+1;
        system.debug('The number of updated records are'+i);
    
}
    update acc;
    //System.debug('The record has the following value'+i);
}

Inner for loop will run  200 times and those 200 records will be added to the collection named acc and then the outer for loop will update 200 recorsd with one DML update statement and same procedure will be repeated for the next set of 200 records and then those 200 records will be updated with dml statement .All in all I have updated  400 records with 2 dml statemen.I dont know in this scenario how can i update 400 records without having dml in the outer for loop.

I understand that in general  dml shoule not be part of for loop.But in above given scenario how can I update 400 records with 2 dml statemenst by not having them inside outer for loop.

Please read the below given comments in bold.|They are from the link which u sent me.

// Use this format for efficiency if you are executing DML statements
// within the for loop.  Be careful not to exceed the 150 DML statement limit
.
 
Account[] accts = new Account[];
 
for (List<Account> acct : [SELECT id, name FROM account
                            WHERE name LIKE 'Acme']) {
    // Your logic here
    accts.add(acct);
}
 
update accts;


Thanks,
Nitin