Adding Attachments to Quick Incidents, Incident & Automation Rules and Tasks

Adding Attachments to Quick Incidents, Incident & Automation Rules and Tasks

Here's some code to allow you to attach existing documents to an incident using Incident Rules, Automation Rules or Tasks. It can also be used to add an attachment to a Quick Incident.

How this works

Attachments are stored in a table in the database called workitem_attachment. The following data is stored in this table:

workitem_number
this refers to the incident number in the workitem table
workitem_version
this is the revision of the incident. For a new incident this will be 1. It increments by one each time an incident is modified.
name
this is the string that will be used in the hyperlink on the helpdesk pages
url
the link to the document's location on the web server
name the physical path to the document on the web server
size
the size of the document in bytes
created_on
the date the document was added to the incident
created_by_worker_id
the id of the worker that added the attachment

In the workitem table itself there is a field called workitem_has_attachments. This is a flag which contains the value 1 or 0. If the value is 1, the workitem view /edit pages search for entries in the workitem_attachment table to locate the attachments associated to the incident.

In a rule or a quick incident we can only set properties directly related to incidents, but we can use the macro HDQUERY in these assignments to run SQL queries against the database. Here we will use this macro to set the has_attachments field for an incident, but by using the HDQUERY macro we also set the values in the workitem_attachment table. The rule will look something like HDQUERY[[INSERT INTO workitem_attachment ... VALUES(....) SELECT 1]]. This query will run the SQL statements which add appropriate data to the workitem_attachment table and then return a value 1, which is the value we want to push back into the has_attachments field in the workitem table.

A practical example

For this example, we will create an incident rule which runs when then Authorize-Approve\Employee category is chosen. The rule will add a pdf form to the incident.

The first thing to do is to create your document. For this example, I am assuming that we have a document called Authorization.pdf which is 80KB in size. Copy this file into the C:\Program Files\Altiris\Helpdesk\AeXHD\upload folder.

Now we can create our rule.

Here we will create an Incident rule, but the process is similar for Automation Rules, Tasks or Quick Incidents. Open the worker console and browse to Admin > Incident Rules > New Incident Rule. Add an appropriate Name and Comment to remind you what this rule does.

In the "Set these properties:" panel, we will add the attachment. Select <Advanced Assignment> from the dropdown list and press Add.

Use the following values:

Set: workitem_has_attachments

To: HDQUERY[[INSERT INTO [workitem_attachment]([workitem_number], [workitem_version], [name], [url], [file_path], [size], [created_on], [created_by_worker_id]) VALUES(WORKITEM(workitem_number), WORKITEM(workitem_version), 'Authorization', 'http://servername/AeXHD/Upload/Authorization.pdf', 'C:\Program Files\Altiris\Helpdesk\AexHD\Upload\Authorization.pdf', 80000, GETDATE(), 40) SELECT 1]]

Type: Number

You will need to modify the the SQL statement in the HDQUERY macro according to your needs. Press OK to return to the previous page.

The conditions are set as normal. In this case we want the rule to be evaluated everytime an Incident is saved.Select Category from the dropdown list and click Add. A new page will appear. Select the checkbox for when the value of "Category" changes and set When the value of Category is the same as Authorize-Approve\Employee Click on OK to return to the previous page. Make sure the Active checkbox is ticked and remove the tick from the Terminal unless you are sure that this is the last rule you want to run for incidents of this type.

Now anytime an incident is opened with the category Authorize-Approve\Employee, the document Authorization.pdf will be added to the incident as an attachment.

3.555555
Average: 3.6 (9 votes)

Attaching variable sized attachments

John,

Thanks for the great tip!

In you example you are using a static attachment and you know the size of the attachments. However, could this be used to attach something a like a weekly log and can the size be variable?

Henry

Dynamic Incidents.

I worked this out so that this could work with multiple and dynamic attachments. I pulled this off by setting this logic to run as an Incident rule when a particular ticket type's parent has attachments.

The parent attachments will trickle down to the child tickets by a SELECT of the workitem_attachment and an INSERT of the same data with the child ticket info substituted.

Cool

That is a good way to do it when you have incidents branching off from automated rules.