Exchange attachments using SOAP in ServiceNow
I came across many scenarios where there is an integration between two ServiceNow systems (not with Integration Hub) and we require to exchange attachments along with the records.
In this post I want to give solution for below use case.
Use case
A bidirectional incident integration configured between 2 ServiceNow systems. We need to enhance this with attachment exchange feature. When an attachment gets added to an incident in one system then that attachment should get attached to the related incident in the other system.
Solution
We can achieve this using eccq, soap message, and a script to trigger this soap message with attachment and incident details.
Assumptions/prerequisites:
- This integration is between 2 ServiceNow instances
- We know the target record sysid in the vendor instance
- Have basic authentication profile handy to authenticate with vendor instance
References:
- Sending an attachment to ServiceNow via Web Services - Servicenowguru
- Create Attachments using GlideSysAttachment() – ServiceNow Community
Solution details:
Create SOAP message with WSDL pointing to vendor instance ecc queue
-
Create a soap message with below details
- WSDL: https://<
>.service-now.com/ecc_queue.do?WSDL - Name: Any understandable name (eg: Attachment to SNOW)
- Authentication type: Basic
- Basic auth profile: Select the basic auth profile to authenticate to vendor instance
- WSDL: https://<
-
Click on related link “Generate sample SOAP messages”
-
Open function “insert” from the related list named “SOAP Message Functions”
- SOAP Endpoint: https://<
>.service-now.com/ecc_queue.do?SOAP - Authentication type: Basic
- Basic auth profile: Select the basic auth profile to authenticate to vendor instance
- SOAP Endpoint: https://<
Create a script to trigger outbound call with attachment
There can be different places where we can write this script and it’s based on your approach. For example, we can write an after BR on attachment table with a filter condition to identify attachment gets added to an integrated incident and write this script in that BR.
var soapRequest = new sn_ws.SOAPMessageV2('Attachment to SNOW', 'insert');
soapRequest.setStringParameter('insert.agent','AttachmentCreator');
soapRequest.setStringParameter('insert.topic','AttachmentCreator');
/*** insert.name will be “anyname.file extension:filetype ***/
soapRequest.setStringParameter('insert.name','testfileupload2.png:image/png');
/*** insert.source will be incident:sysid of the record
for which you want to attach the attachment ***/
soapRequest.setStringParameter('insert.source','incident:8876c30edb723300cbde84da0b9619e2');
/*** If wring this script in BR,
check if you need to query attachment table or
you can simply use current object and
update this script accordingly ***/
var attch_gr = new GlideRecord('sys_attachment');
if(attch_gr.get('55e7e89cdb133300cbde84da0b961933')){
var stringUtil = new GlideStringUtil();
var sa = new GlideSysAttachment();
var binData = sa.getBytes(attch_gr);
// convert it to Encoded Data..
var encData = stringUtil.base64Encode(binData);
soapRequest.setStringParameter('insert.payload',encData);
}
var response = soapRequest.execute();
JSUtil.logObject(soapRequest.getRequestBody() ,'soapRequest ');