Share Your Experience With Others

Asynchronous Trigger

  • Asynchronous Apex triggers are change event triggers that run asynchronously after a database transaction is completed.
  • They are ‘after-insert’ triggers and can be defined with the after insert keywords.
  • The change event object name is suffixed with ChangeEvent For example, Account change event object is named AccountChangeEvent.
  • The change event object name for the custom object is suffixed with __ChangeEvent.
  • When a record is created or updated, Change Data Capture publishes an event and a change event trigger can then process that event asynchronously.
  • Change Data Capture publishes the deltas of Salesforce data for new records or changed records.
  • With asynchronous Apex triggers, we can move complex computations and logic out of the transaction.
  • This feature can be enabled for any supported sObject in your org.
  • Once it is enabled, it starts publishing the events whenever we create, update, delete or undelete a record.
  • The published event contains metadata information about the record and also all the changed field values.
{
  "schema": "90f--o4WxM9RZZ8PKPgJog",
  "payload": {
    "LastModifiedDate": "2019-05-21T17:54:33.000Z",
    "ExpectedRevenue":500002,
    "Amount":5000020,
    "ChangeEventHeader": {
      "commitNumber":10433619776237,
      "commitUser": "005B0000005lfkFIAQ",
      "sequenceNumber":1,
      "entityName": "Opportunity",
      "changeType": "UPDATE",
      "changeOrigin": "com/salesforce/api/soap/46.0;client=SfdcInternalAPI/",
      "transactionKey": "0005e058-5808-e754-5fee-196069fcd60a",
      "commitTimestamp":1558461273000,
      "recordIds": [
        "006B0000004VTRXIA4"
      ]
    }
  }
  • You can see that any field which has modified will have new values in it.
  • Entity Name is the name of object
  • Change type is the operation performed on this object
  • change origin – if the event published inside salesforce then it will contain salesforce in URL. if it is outside then client will contain name of application that publish this event.
  • To capture the debug logs for this trigger, you need to set debug logs under “Automated Process”. Otherwise logs will not show anywhere.
  • The net effect is the transactions become faster and Apex limits consumption is reduced.

Leave a comment