вторник, 6 октября 2015 г.

TriggerTransaction Apex class for cleaner Salesforce triggers


My newly written Salesforce Apex TriggerTransaction class allows you to accumulate changes made to DB in trigger code to minimize DML requests number. Tastes better with kevinohara80/sfdc-trigger-framework.

Dependencies

You need my xforce utility class to use TriggerTransaction.

Usage example

This example uses kevinohara80/sfdc-trigger-framework.

 

Trigger

Create new Apex Trigger named AccountTrigger.apxt with the following code:


trigger AccountTrigger on Account (before insert,
                                   before update,
                                   before delete, 
                                   after insert,
                                   after update,
                                   after delete,
                                   after undelete) {

      new AccountTriggerHandler().run();

}

 

Trigger handler class

Create new Apex Class named AccountTriggerHandler.apxc with the following code:


public class AccountTriggerHandler extends TriggerHandler {

    public override void afterInsert() {
        TriggerTransaction transact = new TriggerTransaction 
                new List<String> {'Account', 'Contact', 'Opportunity'});
        for(Account account_i : (List<Account>) Trigger.new) {
            transact.ups('Contact', new Contact(AccountId = account_i.id,
                                                LastName=account_i.Name
                                                        +' Main Contact'));
        }
        List<Contact> shadowList = transact.shadow([select id, lastName
                                                    from Contact limit 100]);
        X.debug('Shadow contact list', shadowList);
        transact.perform();
      }

}

Комментариев нет:

Отправить комментарий