+ Start a Discussion
ElectronElectron 

Why we should use for loop for trigger? And about the advanced APEX skill.

Thanks to this community provides all the topics and great contributor , especial for Bob, now I write a very good APEX Trigger, and Unit test with 100% Test cover. 

 

But I am still very curious why most people use for loop to initial a trigger.

Such as for(sObject a:tirgger.new)

My trigger only works once update, why we should write it in a for loop?

 

And I think APEX trigger could do a lot work, I want to move to my next milestone, where I could learn more advanced APEX usage? 

Search the method by this guide? 

http://www.salesforce.com/us/developer/docs/apexcode/index.htm 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

This is to ensure the trigger is bulkified - even though you are only expecting it to be called once when the user does something on the UI, it may be that an administrator can hit a number of records in one go, via the import wizard or data loader, which means your trigger would need to process more than one record in a single transaction.

All Answers

bob_buzzardbob_buzzard

This is to ensure the trigger is bulkified - even though you are only expecting it to be called once when the user does something on the UI, it may be that an administrator can hit a number of records in one go, via the import wizard or data loader, which means your trigger would need to process more than one record in a single transaction.

This was selected as the best answer
bob_buzzardbob_buzzard

To answer your second question, I would use the Apex Developer's Guide, Developerforce and blogs to gain advanced skills.

Bhawani SharmaBhawani Sharma
Suppose you have a trigger on Contact object. from the UI part you can insert only one record at a time. But suppose you need to use data loader to insert 100 contacts.
Here Trigger.New comes in picture. At the time of bulk load, trigger processes data in chunks of 200 records. These 200 records are stored in Trigger.New list. Now you can loop through this list and do your processing.
ElectronElectron
Got it, I agree that's the reason.

thank you so much.