PostDeleteInstanceAttachment Trigger
- PostDeleteInstanceAttachment Trigger programs are executed after a Type Instance Attachmentis deleted, after the platform code for attachment deletion executes.
- Instance Attachments are shown in Attachment widget. On deletion of attachment from Attachment widget or from instance the PostDeleteInstanceAttachment Trigger is executed.
- They are used to execute business logic after a instance attachment is deleted. For example, to notify an external system.
- They are executed asynchronously, i.e. the execution takes place after the instance attachment is deleted from the database. Since it is post task, it takes place outside of the database transaction boundary of attachment deletion. It means, the PostDeleteInstanceAttachment trigger cannot prevent an attachment from being deleted.
- Execution of PostDeleteInstanceAttachment 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
-
PostDeleteInstanceAttachment Trigger program is a class that must implement the interface
IPostDeleteInstanceAttachmentTriggerAsyncpublic interface IPostDeleteInstanceAttachmentTriggerAsync
{
Task RunAsync(Guid instanceId, Guid instanceFileId);
}NOTE: Implement IPostDeleteInstanceAttachmentTriggerAsync with the RunAsync method to enable asynchronous, scalable trigger logic. Synchronous APIs (e.g., GetInstance) are deprecated and will be removed. Use their async alternatives and implement RunAsync to ensure compatibility.
-
Protrak runtime passes InstanceId and instanceFileId of the deleted instanceFile 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 PostDeleteInstanceAttachment Trigger
- Use to send notifications to third party systems or email notifications.
Anti-patterns or when not to use PostDeleteInstanceAttachment Trigger
- Do not add business validations in PostDeleteInstanceAttachment Trigger. It should be done in PreUploadInstanceAttachment trigger.
Example program
public class WorkItemPostDeleteInstanceAttachmentTrigger : IPostDeleteInstanceAttachmentAsync
{
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, Guid instanceFileId)
{
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 IPostDeleteInstanceAttachmentTrigger) 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.