Triggers can execute at many different events:

  • before insert
  • before update
  • before delete
  • after insert
  • after update
  • after delete
  • after undelete

Trigger syntax:

trigger TriggerName on ObjectName (trigger_events) {
// code_block
}

Triggers should only be used when declarative tools cannot be. (Why? To save resources?).

What is a handler class?

public with sharing class AccountHandler {
    public static void CreateNewOpportunity(List accts) {
        for (Account a : accts) {
            Opportunity opp = new Opportunity();
            opp.Name = a.Name + ' Opportunity';
            opp.AccountId = a.Id;
            opp.StageName = 'Prospecting';
            opp.CloseDate = System.Today().addMonths(1);
            insert opp;
        }
    }
}

What is this? It seems to be a new class called AccountHandler. Within it there seems to be a method called CreateNewOpportunity which accepts a list? As of yet, I don’t quite understand the public static void part. Then there seems to be a for loop, does it have slightly different syntax to java? So “a” is the placeholder value. Accts is the list? Then it proceeds to create a new opportunity called opp. And the various fields of opp are populated by various values, some of which refer to the account “a”. So it seems like the for loop loops through the list of accounts accts. I feel like there is something missing here in regards to the list.

trigger AccountTrigger on Account (before insert, before update, before
    delete, after insert, after update, after delete,  after undelete) {
    if (Trigger.isAfter && Trigger.isInsert) {
        AccountHandler.CreateNewOpportunity(Trigger.New);
    }
}

Next we have a trigger. So we can see the trigger syntax here. It’s called AccountTrigger and it’s on the Account object. For some reason it seems to be on before all of: before insert, before update, before delete, after insert, after update, after delete and after undelete. Is that something that usually happens, I thought you would do it during just one event. The code block within the trigger is an if statement. So if (Trigger.isAfter && Trigger.isInsert) evaluates to true then the class/method AccountHandler.CreateNewOpportunity then something occurs. Is the placeholder Trigger.new the parameter entered into the method? Is this accts? I’m not sure but I think there’s a rule about the language I’m not getting here.

Account acct = new Account(
    Name='Test Account 2',
    Phone='(415)555-8989',
    NumberOfEmployees=50,
    BillingCity='San Francisco');
insert acct;

This above block of code is something entered into the open execute anonymous window. It creates a new account and populates the values of certain of its fields. Then it inserts the acct which is a DML statement.

I think the relevance of execution context here means that all of these things which have occurred thus far and were executed as displayed in the execution log. That is all included as the execution context and therefore that is what will be subject to governor limits.

Common governor limits involve the number of:

  • SOQL statements (synchronous limit of 150)
  • DML statements (synchronous limit of 100)

Yet triggers can receive up to 200 objects at once. Therefore you can hit what’s called a govenor limit. At this point the developer will have to go back and figure out how to “bulkify” the code. -> bulk pattern.

public with sharing class AccountHandler {
    public static void CreateNewOpportunity(List accts) {
        List opps = new List();
        for (Account a : accts) {
            Opportunity opp = new Opportunity();
            opp.Name = a.Name + ' Opportunity';
            opp.AccountId = a.Id;
            opp.StageName = 'Prospecting';
            opp.CloseDate = System.Today().addMonths(1);
            opps.add(opp);
        }
        if (opps.size() > 0) {
            insert opps;
        }
    }
}

The above presents a bulkified version of the code. This is because the insert DML statement is outside of the for loop. So the for loop compiles everything into one list(?) and then inserts it all at once.

@isTest
private class AccountTrigger_Test {
    @isTest static void TestCreateNewAccountInBulk() {
        // Test Setup data
        // Create 200 new Accounts
        List accts = new List();
        for(Integer i=0; i < 200; i++) {
            Account acct = new Account(Name='Test Account ' + i);
            accts.add(acct);
        }              
        // Perform Test
        Test.startTest();
        insert accts;                               
        Test.stopTest();
        // Verify that 200 new Accounts were inserted
        List verifyAccts = [SELECT Id FROM Account];
        System.assertEquals(200, verifyAccts.size());    
        // Also verify that 200 new Opportunities were inserted
        List verifyOpps = [SELECT Id FROM Opportunity];                              
        System.assertEquals(200, verifyOpps.size());                             
    }
}

This is a test class. Presumably to test the trigger, since that’s what the name is. I’m not sure whether @isTest is part of the code or it’s more of a comment. The comments seem to show what it does. The for loop seems to compile a list of 200 new accounts. And adds them to the list accts which was previously created. Then there are a series of lines of code which insert the list and then verify that the list equals 200.

What is a try-catch-finally block? Apparently, it’s used to handle exceptions.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s