PostUploadInstanceAttachment Trigger
- PostUploadInstanceAttachment Trigger programs are executed after a new Attachment Type Instance is created/ updated, after the platform code for attachment creation/ updation executes.
- They are used to execute business logic after a new attachment is created/ updated. 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 post task, it takes place outside of the database transaction boundary of attachment creation/ updation. It means, the PostUploadInstanceAttachment trigger cannot prevent an attachment from being created/ updated.
- Execution of PostUploadInstanceAttachment 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
-
PostUploadInstanceAttachment Trigger program is a class that must implement the interface
IPostUploadInstanceAttachmentTriggerAsyncpublic interface IPostUploadInstanceAttachmentTriggerAsync
{
Task RunAsync(Guid instanceId, Guid instanceFileId);
}NOTE: Implement IPostUploadInstanceAttachmentTriggerAsync 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 InstanceId and instanceFileId of the created/ updated 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 PostUploadInstanceAttachment Trigger
- Use to send notifications to third party systems or email notifications.
Anti-patterns or when not to use PostUploadInstanceAttachment Trigger
- Do not add business validations in PostUploadInstanceAttachment Trigger. It should be done in PreUploadInstanceAttachment trigger.
Example Program
public class WorkItemPostUploadInstanceAttachmentTrigger : IPostUploadInstanceAttachmentAsync
{
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 IPostUploadInstanceAttachment) 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.