PostDisconnect Trigger
- PostDisconnect Trigger programs are executed after a Relationship Instance is disconnected, after the platform code for disconnect completes.
- They are used to execute business logic after a relationship is removed. For example, to notify an external system, clean up related state, or promote one of the affected instances 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 a post task, it takes place outside of the database transaction boundary of the disconnect operation. It means, the PostDisconnect trigger cannot prevent a relationship from being disconnected.
- Execution of PostDisconnect 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
-
PostDisconnect Trigger program is a class that must implement the interface
IPostDisconnectTriggerProgramAsyncpublic interface IPostDisconnectTriggerProgramAsync{Task RunAsync(Relation relation);}NOTE: Implement
IPostDisconnectTriggerProgramAsyncwith theRunAsyncmethod to enable asynchronous, scalable trigger logic. Most internal service methods are now async, and their synchronous versions are deprecated and will be removed. UseRunAsyncto ensure compatibility with these changes. -
Protrak runtime passes Relation object having information about the instances being disconnected by the trigger.
RelationId: The Guid that uniquely identifies the relationship instance.RelationTypeName: The Relation Type name in Protrak schema using which the instances were connected.Direction: Enum of typeEnum.RelationDirectionhaving valuesToorFromindicating the direction.SourceInstanceId: If the direction isTo, theFrom typeinstance id as per the relation type definition. If the direction isFrom, theTo typeinstance id as per the relation type definition.DestinationInstanceId: If the direction isTo, theTo typeinstance id as per the relation type definition. If the direction isFrom, theFrom typeinstance id as per the relation type definition.RelationAttributes:Attribute[]having the relation attribute values.
Typical use cases for PostDisconnect Trigger
- Perform any operations after the disconnect operation is complete and committed to the database.
- Notify a third-party system that a link between two instances was removed.
- Recalculate or promote one of the affected instances after the relationship is removed.
Anti-patterns or when not to use PostDisconnect Trigger
- Do not use PostDisconnect trigger to make changes that must be part of the same transaction as the disconnect operation. Even if the trigger execution fails, the disconnect transaction will remain as it is in the database and will not be rolled back.
- Do not use it for validation that should prevent the disconnect. Use
PreDisconnectfor checks that must block the operation.
Sample Code
public class AssetToLicensePostDisconnectTrigger : IPostDisconnectTriggerProgramAsync
{
public IInstanceService InstanceService { get; set; }
public IRelationService RelationService { get; set; }
private readonly string ACTION_RELEASE = "Release";
public async Task RunAsync(Relation relation)
{
var assetInstanceId = relation.SourceInstanceId;
var licenseInstanceId = relation.DestinationInstanceId;
// Promote the license to "available" state because it is no longer allocated to an asset.
string comments = $"Released from asset id {assetInstanceId}";
await InstanceService.PromoteInstanceAsync(licenseInstanceId, ACTION_RELEASE, comments);
}
}
Note:
The Run method (from IPostDisconnectTriggerProgram) should only be used if the trigger logic is fully synchronous, does not involve any service/API calls, and consists only of lightweight in-memory operations. For the post-disconnect server path, the actual dispatch is asynchronous and goes through the post-commit event handler after the relationship delete is committed.