Triggers are used to automatically perform an action when an event occurs. For example, when an account is deleted, you can set a trigger that will delete all contacts that are related to that account. Triggers can be used on Insert, on Delete, and on Update. They can be used before or after a database event occurs.
Trigger Event Type:
Trigger Event Mode:
|IsInsert||Is "true" if the trigger was fired as a result of Insert operation.|
Is "true" if the trigger was fired as a result of Update operation.
|IsDelete||Is "true" if the trigger was fired as a result of Delete operation.|
|IsBefore||Occurs before the record changes are applied.|
|IsAfter||Occurs after the record changes are applied.|
|Records||A list of RecordContext which each RecordContext contains two properties: "New" and "Old".|
"New" is available on "Insert" and "Update" modes.
"Old" Is available on "Update" and "Delete" modes.
Note: IsBefore does not fire on Records changes on extension Entities (such as Salesforce CRM) when the change is originated from those external systems.
Below is an example of deleting all contacts related to the account when an account is deleted:
public class onAccountDelete : ActiveTrigger<Account>
public override void Execute(TransactionContext<Account> trigger)
if (trigger.IsDelete && trigger.IsAfter)
List<string> accountIDs = trigger.Records.Select(a=>a.Old.Id).ToList();
List<Contact> contacts = Database.Query<Contact>(c => accountIDs.Contains(c.AccountId)).ToList();
if (contacts != null) Delete(contacts);
Below is an example of creating a new payment when a new order is made:
public class OnNewOrderTrigger : ActiveTrigger
public override void Execute(TransactionContext<Order> trigger)
if (trigger.IsInsert && trigger.IsAfter)
List<Payment> payments = new List<Payment>();
foreach(var recordContext in trigger.Records)
var order = recordContext.New;
Payment p = new Payment();
p.OrderId = order.Id;
p.AccountId = order.AccountId;
p.Amount = order.TotalCost;