PostConnect Trigger
- PostConnect Trigger programs are executed after a new Relationship Instance is created.
- They are used to execute business logic after a new Relationship 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 Relationship instance is committed to the database. Since it is an out of process task, it takes place outside of the database transaction boundary of instance creation. It means, the PostConnect trigger cannot prevent a Relationship instance from being created.
- Execution of PostConnect 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
-
PostConnect Trigger program is a class that must implement the interface
IPostConnectTriggerProgramAsyncpublic interface IPostConnectTriggerProgramAsync
{
Task RunAsync(Relation relation);
}NOTE: Implement IPostConnectTriggerProgramAsync 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
RunAsyncto ensure compatibility with these changes. -
Protrak runtime passes Relation object having information about the instances being linked to the trigger.
RelationId: The Guid that uniquely identifies the relationship instance.RelationTypeName: The Relation Type name in Protrak schema using which the instances are being connectedDirection: Enum of type Enum.RelationDirection having values "To" or "From" indicating the direction. The direction will decide which instance type will be present in source and destination instances.SourceInstanceId: If the direction is "To" the "From type" instance id as per the relation type definition. If the direction is "From", the "To type" instance id as per the relation type definition.DestinationInstanceId: If the direction is "To" the "To type" instance id as per the relation type definition. If the direction is "From", the "From type" instance id as per the relation type definition.RelationAttributes:Attribute[]having the relation attribute values.
Typical use cases for PostConnect Trigger
- Perform any operations after the Link operation is complete and committed to the database. For example, if a user or an external system needs to be notified.
Anti-patterns or when not to use PostConnect Trigger
- Do not use PostConnect trigger to make changes that must be part of same transaction as the connect operation. Even if the trigger execution fails, the connect transaction will remain as it is in the database and will not be rolled back.
Sample Code
public class AssetToLicensePostConnectTrigger : IPostConnectTriggerProgramAsync
{
public IInstanceService InstanceService { get; set; }
public IRelationService RelationService { get; set; }
private readonly string ACTION_ALLOCATE = "Allocate";
public async Task Run(Guid relationId)
{
var relation = RelationService.GetRelation(relationId);
var assetInstanceId = relation.SourceInstanceId;
var licenseInstanceId = relation.DestinationInstanceId;
//Promote the license to "in use" state as it is now allocated to an asset
string comments = $"Allocated to asset id {assetInstanceId}";
await InstanceService.PromoteInstanceAsync(licenseInstanceId, ACTION_ALLOCATE, comments);
}
}
Note:
The Run method (from IPostConnectTriggerProgram) 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.