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
Tamojita GuhasarkarTamojita Guhasarkar 

Doubt Clear on SFDC Governor limit - bulkfy DML

Hello Folks ,

Please help me to clear few bacis doubts related to Governor limit - bulkfy DML 

Case 1: My code is like below

List<Account> act = [ some query and it retrns 5000 records ];
for ( account a : act)
{
a.description =' updated by script';
}
update act;

Case 2: My code is like below
List<Account> act = [ some query and it retrns 5000 records ];
List<Account> new_act = new List<Account>();

for ( account a: act)
{
a.description =' updated by script ';
new_act.add(a);
}

update new_act;

================================

Ok so my question is as my quesry is retriving 5000 records and I want to update these records ,so I needto Bultyfly my dml so it will not breach the govornor limit . so in to achive this , as per my understading , both of these approches should do the same work , i.e , as I worte the Update outside the loop, hence it will execute only once ( though the for loop goes for 5000 times ) and update all these 5000 records. 

So is this correct ? can someone clafiry please .

Thanks,
Tanoy
Best Answer chosen by Tamojita Guhasarkar
brahmaji tammanabrahmaji tammana

Hi,

Your example does not explain Bulkification. 
Because, 
Case 1: Updates all accounts even if does not enter into for loop.
Case 2: Here you used the collection and the update operation will be performed only on the records which are in the List. 

Both cases will do the same job. It is not an example of Bulkifyng.

Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time. When a batch of records initiates Apex, a single instance of that Apex code is executed, but it needs to handle all of the records in that given batch. For example, a trigger could be invoked by an Force.com SOAP API call that inserted a batch of records. So if a batch of records invokes the same Apex code, all of those records need to be processed as a bulk, in order to write scalable code and avoid hitting governor limits.

Check this link for more details:
https://developer.salesforce.com/page/Apex_Code_Best_Practices

Hope it helps !!

Thanks

Brahma

All Answers

RD@SFRD@SF
Hi Tanoy,

Yes you are correct that, both the code would do the same.
As a best practice, the list used to get the records is not altered. 
In your case List act, as you might need to query the accounts later.
Just imagine the query returns 5000 records.

1. If you have nothing else to do the code one would be fine. But eventually the code grows, triggers are added and in a single transaction only 100 dmls are allowed. Hence for making the code  "future proof" the query result is kept, then again depends on the requirement.

2. If you are query result is 5000, but then there is a condition and only 10 of the records satisfy.
    a) In code 1 : update runs for 5000 records
    b) In code 2 : update runs only for 10 records as the new list would only have records which satisfy the condition,
3. Say you are using differernt "Record types", then each record type might have different fields.
    a) We would end up using different Database calls. Note: Best approach is to always reduce the calls to the database
    b) Say there are 5 records types, 5 database calls would be needed. Instead do one call and get the result.
    c) Next create 5 list variable of the object type. and use if else condition to sort them into the list by comparing the record types.
    d) Thus reducing the DML operations.

Hope it helps to give a picture
Rd
brahmaji tammanabrahmaji tammana

Hi,

Your example does not explain Bulkification. 
Because, 
Case 1: Updates all accounts even if does not enter into for loop.
Case 2: Here you used the collection and the update operation will be performed only on the records which are in the List. 

Both cases will do the same job. It is not an example of Bulkifyng.

Bulkifying Apex code refers to the concept of making sure the code properly handles more than one record at a time. When a batch of records initiates Apex, a single instance of that Apex code is executed, but it needs to handle all of the records in that given batch. For example, a trigger could be invoked by an Force.com SOAP API call that inserted a batch of records. So if a batch of records invokes the same Apex code, all of those records need to be processed as a bulk, in order to write scalable code and avoid hitting governor limits.

Check this link for more details:
https://developer.salesforce.com/page/Apex_Code_Best_Practices

Hope it helps !!

Thanks

Brahma

This was selected as the best answer
Ramssf70Ramssf70
Hi Tamojita Guhasarkar ,

Both are from same purpose

 
for ( account a : act)
{
a.description =' updated by script';
}
update act;

//Now it will update one record

for(account acc:acclist)
{

a.description =' updated by script';


}

update acclist;

//Now it will update List of records available in  the list

List<Account> act = [ some query and it returns 5000 records ];
List<Account> new_act = new List<Account>();

for ( account a: act)
{
a.description =' updated by script ';
new_act.add(a);
}


//now also you are updating number of records in the list.What you are doing in this scenario is you can use both

 
Tamojita GuhasarkarTamojita Guhasarkar
sorry for delay !! thanks folks for helping me out . got my answer..