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
sandhya santhanagopalansandhya santhanagopalan 

accounts update trigger

list<Account> lstAccounts = new list<Account>();
for(order__c objorder:trigger.new){
account a = [select id from Account where id=:objorder.Account__c];
lstAccounts .add(a);
}
update lstAccounts;
How many order will be load when developer attempts to load 150 records.
Best Answer chosen by sandhya santhanagopalan
Lokesh KumarLokesh Kumar

HI Sandhya,

Trigger run in a Batch mode so Trigger.New always contains 200 records at one time so if more than 200 ex 500 then it runs three time in the batch of 200.

"Triggers execute on batches of 200 records at a time. So if 400 records cause a trigger to fire, the trigger fires twice, once for each 200 records." 

So it's never more than 200 records at a time.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bulk_idioms.htm

Thanks 
Lokesh

All Answers

Lokesh KumarLokesh Kumar

HI Sandhya,

Trigger run in a Batch mode so Trigger.New always contains 200 records at one time so if more than 200 ex 500 then it runs three time in the batch of 200.

"Triggers execute on batches of 200 records at a time. So if 400 records cause a trigger to fire, the trigger fires twice, once for each 200 records." 

So it's never more than 200 records at a time.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bulk_idioms.htm

Thanks 
Lokesh

This was selected as the best answer
sandhya santhanagopalansandhya santhanagopalan
Thanks Lokesh.
So the trigger loads only once here as thr r only 150 records.
sandhya santhanagopalansandhya santhanagopalan
So the trigger gets executed only once right?
the options for the above were

150
100
1
0
Lokesh KumarLokesh Kumar
yes it will execute only once as you have 150 records.

Thanks 
Amit Chaudhary 8Amit Chaudhary 8
Trigger will execute only once and it will qeury 150 times.

That will better if you will use Map for trigger like below
Set<ID> setAccID = new Set<Id>();
for(order__c objorder : trigger.new)
{
	setAccID.add(objorder.Account__c);
}

if( setAccID.size() >0 )
{	
	Map<Id,Account> accountMap  = new Map<Id,Account> ( [select id from Account where id=:setAccID ] );
	list<Account> lstAccounts = new list<Account>();

	for(order__c objorder : trigger.new)
	{
	
		if(accountMap.containsKey(objorder.Account__c) )
		{
			account a = accountMap.get(objorder.Account__c);
			lstAccounts .add(a);
		}
	}

	if(lstAccounts.size() > 0 )
	{
		update lstAccounts;
	}
}


Let us know if this will help you