mail-attachment-md

Please Note: The functionality described in this post is no longer necessary for those using the “Geneva” (or later) release of ServiceNow. I highly recommend people use the “Attachment API” feature introduced in that release.

I had a friend ask me the other day if we could submit attachments through ServiceNow via a RESTful interface rather than through the documented SOAP endpoint. I had to ponder it for a minute before I realized that we could definitely connect to the AttachmentCreator web service via REST just as we do with the SOAP protocol. Please note, however, that as with any of the REST API in ServiceNow, your instance will need to have the “JSON Web Service” plugin enabled.

The ServiceNow logo used in my test as an attachment.

The ServiceNow logo used in my test as an attachment.

I ran a quick test on a “ServiceNow” logo and attached it to one of my existing incidents in the following manner:

Step 1:

Convert the file to Base64 format. Depending on your programming language, there should be libraries that do this for you. Base64 encoding allows you to take a binary object and represent it as a string.

With this logo file, the base64 string representation is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
R0lGODlh4AAsANUgAIuMjNjY2LGysumRlfX19W5vb8XFxXd4eN5aYZ6fn+zs7IGCgs7Pz/zx8qip
qffW1+Li4uaEifTIyuR2e9YxOdk/RpWVleFobu6ssPG6vbu7u9tMVPnj5eyeotMjLGRlZf///wAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAACAALAAAAADgACwAAAb/QJBw
SCwaj8ikcslsOp/QqHRKrVqv2Kx2y+16v+CweEwum8/otHrNbrvf8Lh8Tq/b7/i8fs/v+/+AgYJl
DA4Ah4cCAUwKGgmIAA4MSAECBkMGFoiXIAIACQRNEI+TBI8CSRCekAKTTAQGj4iSSRkDGA1EGREI
vRO4UBEeHhNKDR0IEw9VGb0YUwYFH9PU1AecRgoJ1dwHrkTUAgQL3B+K1ABNB9Sm1ItFDOvl0wca
SQQC0vMFqEUDwx4q5MpQAaBBDwOcPDD47AgHCgApcJiSweAGELx8RWh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X3oqhTigHqNQfFHvcBwGwLEWVptc+/zpvMBqk
BOI3V9azpwo77kEDP1e1Z8SOU7AFAYW0VeiRrjsROBMoDxHUsq8vQrIT9H6QjhQzHx5j5B4UjRFE
3wpxvHhDuCgB+ctAPEDbLHLBJATdIxrF9EyY5Fr3KfvbTZTzqyQwQADGYRmjnhC+IyAOYjdSAs8Y
B4L1DUBWARECniS3Pvt84SwBuNc0AHUPB2CDf1GjRl42BKIvXWg5GSrClh5lnKAUEAkJAFQDjYC4
DUouYzk6iMTa5iKcVTBhBnmWE0BGNRzeZEMFwJ4R6MIJFCqhbyL8QIa+NMMJKSFTi3ghcpYgIe3s
EDR22pYHLgBENbIKW4uRGAT5JIVyTf8thgPyHwhYFsUkbOh7VlSCcoh1hC91ohqgcCI1XNPFR3ls
CGJM2RmJgDg4JgxWcUJVBImAQcvdaTFGJFc1rtGggNFECC9TGk6g4zTUPYFTUxLgGIVgGFISgQEe
+h4IuocS7OGyGgKa5BAqmbulaHIYoYScYpKZtqBNYW4AsIAh/nKdoz3KLOXoUiCVQJhDIcGQAzLM
NBZwCKQtEJbXLCdZmiPMyykOBGrszouM8DMf8UgxQHyF3ebRDyKwLi5dAsE2k/DPD5BQlrDDyT67
IUV0huQATmonCIipTHkiEwk+HIbCiDBBh1lBA50b5SPBtNBDHXSgSGCZFpEARiNooJuV3HAAHhVQ
Un4MB2IbPYKLxjWfYzosc0ZYH2WMgCoPWqEQADDJJ0YqGEPooyy0MIKE+vkE2FwjFesYChJicZYF
WAB3ldIAAILyCbAW4TwVkNgRCAKfZBLhAaSRVQUQMAC1YnRPFJgAEB+AV70O4q+ADaxgB0vYwhr2
sIhNrGIXy9jGOvaxkI2sZCdL2cpa9rKYzewQggAAOw==

Step 2:

Prepare your REST post in the following manner:

POST URL
https://[YOURINSTANCE].service-now.com/ecc_queue.do?JSON&sysparm_action=insert

POST Body
{
“agent”:”AttachmentCreator”,
“topic”:”AttachmentCreator”,
“name”:”[FILENAME]:[MIME_TYPE_FOR_FILENAME]”,
“source”:”[TABLE_NAME]:[SYS_ID_OF_RECORD_TO_ATTACH_TO]”,
“payload”:”[INSERT_BASE64_STRING_HERE]”
}

For my example, the POST Body looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
   "agent":"AttachmentCreator",
   "topic":"AttachmentCreator",
   "name":"now.gif:application/gif",
   "source":"incident:9c573169c611228700193229fff72400",
   "payload":"R0lGODlh4AAsANUgAIuMjNjY2LGysumRlfX19W5vb8XFxXd4eN5aYZ6fn+zs7IGCgs7Pz/zx8qip
qffW1+Li4uaEifTIyuR2e9YxOdk/RpWVleFobu6ssPG6vbu7u9tMVPnj5eyeotMjLGRlZf///wAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAACAALAAAAADgACwAAAb/QJBw
SCwaj8ikcslsOp/QqHRKrVqv2Kx2y+16v+CweEwum8/otHrNbrvf8Lh8Tq/b7/i8fs/v+/+AgYJl
DA4Ah4cCAUwKGgmIAA4MSAECBkMGFoiXIAIACQRNEI+TBI8CSRCekAKTTAQGj4iSSRkDGA1EGREI
vRO4UBEeHhNKDR0IEw9VGb0YUwYFH9PU1AecRgoJ1dwHrkTUAgQL3B+K1ABNB9Sm1ItFDOvl0wca
SQQC0vMFqEUDwx4q5MpQAaBBDwOcPDD47AgHCgApcJiSweAGELx8RWhQIcORbfO4JTDCQF/IaSPB
0SNXDoCCau+SGLAGAoA7IgQsnKy2IFQR/wjydgLwKeQfwAkTDirdkGtJB4MJj0g4GGHKhoMgOEzY
8ACBB2FRiQjgVuAQy2rYQMxsGZRaSiE7p6XTiXKJzWkOat4UMm5eW2o9iUAwCfgsYKJGlSoGeMEI
BwkShiRGOOSBhIkgGhy8KB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DUBWARECniS3Pvt84SwBuNc0AHUPB2CDf1GjRl42BKIvXWg5GSrClh5lnKAUEAkJAFQDjYC4
DUouYzk6iMTa5iKcVTBhBnmWE0BGNRzeZEMFwJ4R6MIJFCqhbyL8QIa+NMMJKSFTi3ghcpYgIe3s
EDR22pYHLgBENbIKW4uRGAT5JIVyTf8thgPyHwhYFsUkbOh7VlSCcoh1hC91ohqgcCI1XNPFR3ls
CGJM2RmJgDg4JgxWcUJVBImAQcvdaTFGJFc1rtGggNFECC9TGk6g4zTUPYFTUxLgGIVgGFISgQEe
+h4IuocS7OGyGgKa5BAqmbulaHIYoYScYpKZtqBNYW4AsIAh/nKdoz3KLOXoUiCVQJhDIcGQAzLM
NBZwCKQtEJbXLCdZmiPMyykOBGrszouM8DMf8UgxQHyF3ebRDyKwLi5dAsE2k/DPD5BQlrDDyT67
IUV0huQATmonCIipTHkiEwk+HIbCiDBBh1lBA50b5SPBtNBDHXSgSGCZFpEARiNooJuV3HAAHhVQ
Un4MB2IbPYKLxjWfYzosc0ZYH2WMgCoPWqEQADDJJ0YqGEPooyy0MIKE+vkE2FwjFesYChJicZYF
WAB3ldIAAILyCbAW4TwVkNgRCAKfZBLhAaSRVQUQMAC1YnRPFJgAEB+AV70O4q+ADaxgB0vYwhr2
sIhNrGIXy9jGOvaxkI2sZCdL2cpa9rKYzewQggAAOw=="

}

The Response

After posting, I got the following JSON string back as the response to my request:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
   "records":[
      {
         "agent":"AttachmentCreator",
         "agent_correlator":"null",
         "error_string":"null",
         "from_host":"null",
         "from_sys_id":"null",
         "name":"now.gif:application/gif",
         "payload":"Attachment created and attached to 'incident' record with sys_id: '9c573169c611228700193229fff72400'",
         "processed":"",
         "queue":"input",
         "response_to":"null",
         "sequence":"13f5759f6970000001",
         "source":"incident:9c573169c611228700193229fff72400",
         "state":"processed",
         "sys_created_by":"admin",
         "sys_created_on":"2013-06-18 12:54:41",
         "sys_id":"899ed425b8428100f8e677b6d50cd416",
         "sys_mod_count":"0",
         "sys_updated_by":"admin",
         "sys_updated_on":"2013-06-18 12:54:41",
         "topic":"AttachmentCreator"
      }
   ]
}

The Final Result

I was able to verify that the corresponding incident record was updated with the file attachment.

ScrnAttached