Pages

Tuesday, September 6, 2016

Salesforce: Getting Started with Trigger syntax


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

TriggerName: name for the trigger e.g. AccountHandler
ObjectName: object start the trigger e.g. Account
trigger_eventscan be a comma-separated list of one or more of the following events:
before insert
- before update
- before delete
- after insert
- after update
- after delete
- after undelete

For example, the following code defines a trigger for the before insert and before update events on the Account object:
trigger AccountHandler on Account (before insert, before update) {
    // Your code here
}

Context Variables
To access the records that caused the trigger to fire, use context variables.
Trigger.New contains all the records that were inserted in insert or update triggers.
Trigger.Old provides the old version of values before they were updated in update triggers, or a list of deleted values in delete triggers.

You can use for loop to iterate over Trigger.New to get each individual sObject because data enter to Salesforce maybe in bulk, example: Data Loader or from integration with other system. Sample trigger below will populate / overwrite Description field when new accounts created.

trigger AccountHandler on Account (before insert) {
    for (Account a : Trigger.New) {
        a.Description = 'Hello World';
    }   
}
a = variable of object Account

Sample of after insert trigger:
trigger AccountHandler on Account (after insert) {
    for (Account acc : Trigger.New) {
        Task t = new Task();
        t.Subject = 'Hello World';
        t.Priority = 'High';
        t.WhatId = acc.Id;
        insert t;   
    }   
}
compare this trigger with before insert in trigger earlier, this trigger will create a Task and relate it to Account created, therefore we need Account Id.

NOTES: in triggers above, we directly put the logic into trigger, it will work, but that is not the best practice. Ideally trigger should just act as traffic controller to direct to particular apex class based on the event and context.

Here another sample of trigger using if and else syntax, and trigger context variables.
trigger AccountHandler on Account (before insert, after insert, after delete) {
    if (Trigger.isInsert) {
        if (Trigger.isBefore) {
            // Process before insert
        } else if (Trigger.isAfter) {
            // Process after insert
        }        
    }
    else if (Trigger.isDelete) {
        // Process after delete
    }
}

List of context variables available for triggers:
  • isExecuting: returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
  • isInsert: returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
  • isUpdate: returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
  • isDelete: returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
  • isBefore: returns true if this trigger was fired before any record was saved.
  • isAfter: returns true if this trigger was fired after all records were saved.
  • isUndelete: returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)
  • new: returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.
  • old: returns a list of the old versions of the sObject records. Note that this sObject list is only available in update and delete triggers.

Reference


No comments:

Post a Comment

Page-level ad