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
NikithaNikitha 

hey I'm new to this. I don't know how to write bulk triggers. whenever I'm trying to write bulk trigger always getting err message. And why we should use Set and Map in bulk trigger. How to define bulk trigger in helper class.

Malika Pathak 9Malika Pathak 9

Hi Swathi,

In simple terms,
Set: A set is an unordered collection of primitives or sObjects that do not contain any duplicate elements.
So, use set if you want to make sure that your collection should not contain Duplicates.
EX: Set<account>

Map: A map is a collection of key-value pairs where each unique key maps to a single value. Keys can be any primitive data type, while values can be a primitive, sObject, collection type or an Apex object. For example, the table represents a map of countries and currencies

To write bulk safe triggers, it is critical that you understand and utilize sets and maps. Sets are used to isolate distinct records, while maps are name-value pairs that hold the query results retrievable by record id. So here is the bulk safe trigger code.
 

trigger AddOwnerColor on Account (before insert, before update) {

    // create a set of all the unique ownerIds
    Set<id> ownerIds = new Set<id>();
    for (Account a : Trigger.new)
        ownerIds.add(a.OwnerId);   

    // query for all the User records for the unique userIds in the records
    // create a map for a lookup / hash table for the user info
    Map<id, User> owners = new Map<id, User>([Select Favorite_Color__c from User Where Id in :ownerIds]);  

    // iterate over the list of records being processed in the trigger and
    // set the color before being inserted or updated
    for (Account a : Trigger.new)
        a.Owner_Favorite_Color__c = owners.get(a.OwnerId).Favorite_Color__c;

}

To test the trigger
@isTest
private class TestAddOwnerColor {

    static testMethod void testBulkInsert() {

        List<account> accounts = new List<account>();

        Profile p = [select id from profile where name='Marketing User'];
        // create a user to run the test as
        User u = new User(alias = 'test123', email='test1234@noemail.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id, country='United States',
            Favorite_Color__c='Buttercup Yellow',
            timezonesidkey='America/Los_Angeles', username='test1234@noemail.com');
        insert u;

        Profile p1 = [select id from profile where name='Standard User'];
        // create a user to own the account
        User u1 = new User(alias = 'test123', email='test12345@noemail.com',
            emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p1.Id, country='United States',
            Favorite_Color__c='Pretty Pink',
            timezonesidkey='America/Los_Angeles', username='test12354@noemail.com');
        insert u1;

        // add 200 accounts to the list to be inserted
        for (Integer i=0;i<200;i++) {

            Account a = new Account(
                Name = 'Test Account',
                OwnerId = u1.Id
            );
            accounts.add(a);

        }

        // Switch to the runtime context
        Test.startTest();  

        // run as a different user to test security and rights
        System.runAs(u) {
            insert accounts;
        }

        // Switch back to the original context
        Test.stopTest();     

        // query for all accounts created and assert that the color was added correctly
        for (Account acct : [Select Owner_Favorite_Color__c from Account Where OwnerId = :u1.Id])
            System.assertEquals(acct.Owner_Favorite_Color__c,'Pretty Pink');

    }
}

If you find this solution is helpful for you please mark the best answer.