Table of Contents


Triggers

Triggers are used to automatically perform actions when specific data events occur in the Magentrix platform. For example, when an Account is deleted, you can configure a trigger to delete all related Contacts automatically.

Triggers can execute logic on the following events:

  • Insert
  • Update
  • Delete

They can also run before or after the database transaction is committed.


Trigger Event Type:

trigger.IsInsert   // True if the operation is an Insert
trigger.IsUpdate   // True if the operation is an Update
trigger.IsDelete   // True if the operation is a Delete

 

Trigger Event Mode:

trigger.IsBefore   // Executes before the changes are saved
trigger.IsAfter    // Executes after the changes are saved

 

Properties

NameDescription
IsInsertTrue if the trigger was fired due to an Insert operation.
IsUpdateTrue if the trigger was fired due to an Update operation.
IsDeleteTrue if the trigger was fired due to a Delete operation.
IsBeforeRuns before the record changes are committed to the database.
IsAfterRuns after the record changes have been saved in the database.
RecordsA list of RecordContext objects. Each RecordContext contains two properties:
"New" – Available during Insert and Update operations.
"Old" – Available during Update and Delete operations.
Note:IsBefore triggers only fire on native Magentrix entities. They do not execute on changes made to CRM-synced entities (e.g., Salesforce or Dynamics).
Additionally IsDelete does not fire for CRM entities.

Example: Deleting all Contacts related to an Account when the Account is deleted

public class onAccountDelete : ActiveTrigger<Account>
{
   public override void Execute(TransactionContext<Account> trigger)
   {
      if (trigger.IsDelete && trigger.IsAfter)
      {
         var accountIds = trigger.Records.Select(a => a.Old.Id).ToList();

         var contacts = Database.Query<Contact>()
              .Where(c => accountIds.Contains(c.AccountId))
              .ToList();

         if (contacts != null) 
            Delete(contacts);
      }
   }
}

Example: Creating a new Payment when a new Order is created

public class OnNewOrderTrigger : ActiveTrigger<Order>
{
   public override void Execute(TransactionContext<Order> trigger)
   {
      if (trigger.IsInsert && trigger.IsAfter)
      {
         var payments = newList<Payment>();

         foreach (var recordContext in trigger.Records)
         {
            var order = recordContext.New;

            var p = new Payment {
               OrderId = order.Id,
               AccountId = order.AccountId,
               Amount = order.TotalCost
            };

            payments.Add(p);
         }

         Create(payments);
      }
   }
}

Example: Validating required fields before saving a Project Task

public class OnProjectTaskValidation : ActiveTrigger<ProjectTask>
{
   public override void Execute(TransactionContext<ProjectTask> trigger)
   {
      if (trigger.IsBefore && (trigger.IsInsert || trigger.IsUpdate))
      {
         foreach (var recordContext in trigger.Records)
         {
            var task = recordContext.New;

            if (task.Status == "In Progress" && task.EstimatedEffort == null)
            {
               // First parameter is the field name, second is the error message
               task.AddError("EstimatedEffort", "Estimated effort should be provided");
            }
         }
      }
   }
}