Skip to main content

PostCreate Trigger

  • PostCreate Trigger programs are executed after a new Protrak Type Instance is created, after the platform code for instance creation executes.
  • They are used to execute business logic after a new type instance is created. For example, to notify an external system or to promote the instance based on business requirements.
  • They are executed asynchronously, i.e. the execution takes place after the instance is committed to the database. Since it is an post task, it takes place outside of the database transaction boundary of instance creation. It means, the PostCreate trigger cannot prevent an instance from being created.
  • Execution of PostCreate trigger program is an asynchronous activity. There may be some small delay in execution, depending on the number of messages already in the queue.

Coding guidelines

  • PostCreate Trigger program is a class that must implement the interface IPostCreateTriggerProgramAsync

    public interface IPostCreateTriggerProgramAsync
    {
    Task RunAsync(Guid instanceId);
    }

    NOTE: Implement IPostCreateTriggerProgramAsync with the RunAsync method to enable asynchronous, scalable trigger logic. Most internal service methods are now async, and their synchronous versions are deprecated and will be removed. Use RunAsync to ensure compatibility with these changes.

  • Protrak runtime passes InstanceID guid of the created instance to the trigger.

  • In case of failure in execution of the trigger even after the standard number of retries, the message is sent to dead letter queue and can be re-inserted in queue only by a system administrator. Hence it is very important to write robust code.

Typical use cases for PostCreate Trigger

  • Use to send notifications to third party systems or email notifications.
  • Create and connect child instances for a type automatically.
    • For example, creates task child instances on creation of project.

Anti-patterns or when not to use PostCreate Trigger

  • Do not add business validations in PostCreate Trigger. It should be done in PreCreate trigger.

Example Program

public class WorkItemPostCreateTrigger : IPostCreateTriggerProgramAsync
{
public IInstanceService InstanceService { get; set; }
public INotificationService NotificationService { get; set; }

private readonly string ATTRIBUTE_TECHNICAL_CONSULTANT = "TechnicalConsultant";
private readonly string ATTRIBUTE_PROJECT_OWNER = "ProjectOwner";

public async Task RunAsync(Guid instanceId)
{
var attributeNames = new string[] { ATTRIBUTE_TECHNICAL_CONSULTANT, ATTRIBUTE_PROJECT_OWNER };
var projectInstance = await InstanceService.GetInstanceAsync(instanceId, attributeNames);

SendNotification(projectInstance);
}

private void SendNotification(Instance instance)
{
//business logic here
NotificationService.SendNotification(content);
}
}

Note: The Run method (from IPostCreateTriggerProgram) should only be used if the trigger logic is fully synchronous, does not involve any service/API calls, and consists only lightweight in-memory operations.