Skip to main content

PostUpdateRelation Trigger

  • PostUpdateRelation Trigger programs are executed after a Relationship Instance updated, after the platform code for relationship instance update executes.
  • They are used to execute business logic after a relationship instance is updated. For example, to notify an external system or to promote the related instances based on business requirements.
  • They are executed asynchronously, i.e. the execution takes place after the relationship instance update is committed to the database. Since it is an post task, it takes place outside of the database transaction boundary of relationship instance update. It means, the PostUpdateRelation trigger cannot prevent an relationship instance from being updated.
  • Execution of PostUpdateRelation 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

  • PostUpdateRelation Trigger program is a class that must implement interface IPostUpdateRelationTriggerProgramAsync

    public interface IPostUpdateRelationTriggerProgramAsync
    {
    Task RunAsync(Guid relationId);
    }

    NOTE: Prefer implementing IPostUpdateRelationTriggerProgramAsync 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 RelationId guid of the relationship 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 PostUpdateRelation Trigger

  • Notify an external system of the update operation.
  • Promote the connected source and destination instances.

Anti-patterns or when not to use PostUpdateRelation Trigger

  • Do not add business validations in PostUpdateRelation Trigger. It should be done in PreUpdateRelation trigger.

Sample Code

public class ProjectPostUpdateRelationshipInstanceProgram : IPostUpdateRelationTriggerProgramAsync
{
public IRelationService RelationService { get; set; }
public IInstanceService InstanceService { get; set; }
public INotificationService NotificationService { get; set; }

private readonly string TYPE_PROJECT = "Project";
private readonly string TYPE_WorkItem = "WorkItem";
private readonly string ATTRIBUTE_TECHNICAL_CONSULTANT = "TechnicalConsultant";
private readonly string ATTRIBUTE_PROJECT_OWNER = "ProjectOwner";

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

SendNotification(projectInstance);
}

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

Note: The Run method (from IPostUpdateRelationTriggerProgram) 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.