• 1Installation
  • 2Preliminary Knowledge
  • 3Create Triggers
  • 4Trigger Handler
  • 5Bulk Object
  • 6Find Specific Objects
  • 7Data Sharing
  • 8Catch All Events
  • 9Trigger Controller
  • 10Normal Trigger Event Handler
  • 11Trigger Context
  • 12Unit Test
  • 13Take Only Trigger Execution

Trap.apex

  • Docs
  • Tutorials
Getting started with Trap.apex

Data Sharing

You should not do data query inside a loop in the trigger. So is also true in Trap.apex. Bulk objects manage shared data for you, so that data can be accessed globally in the streams. Here is an example.

bulkObj.newStream
    .tap(bulkObj.data('accounts', new GetAccountsFunc()))
    .subscribe(bulkObj.provide('accounts', new CustomFunc()));

We compute the account list from GetAccountsFunc and set it to the data in the bulk object. Then we provide the data to CustomFunc from the bulk object.

Here is how we compute the account list from GetAccountsFunc.

public class GetAccountsFunc extends Func {
    public GetAccountsFunc() {
        super(1);
    }

    public override Object exec(Object arg) {
        Map newMap = (Map)arg;
        return [ SELECT Id FROM Account WHERE Id IN :newMap.keySet() ];
    }
}

And we use the account list in our CustomFunc.

public class CustomFunc extends Func {
    public CustomFunc() {
        super(2);
    }

    public override Object exec(Object arg1, Object arg2) {
        List accountList = (List)arg1;
        SObject sObj = (SObject)arg2;
        // Custom code
        return ...;
    }
}

Data set to the bulk object will only be set once, so that it will not cause extra execution for every item through the stream.

Done