jason_petty
Tera Expert

I recently had a customer want to get the attachments from an incident but in a "pull" fashion rather than ServiceNow pushing them to the third party software we were integrating with. This presented a new challenge. Chris Maloy helped me come up with a solution in two different ways that I wanted to share. I also share this so I can look it up later since I will forget it and when I find my own blog, I will think, "I wrote this?"

Scripted Web Service

The advantage of using a scripted web service is that you can use the common tools to issue your SOAP request but you can control what is returned.

  1. Create a new Scripted Web Service

    1. Click on System Web Services | Scripted Web Services
    2. Click New button
    3. Set the Name (This will determine what the WSDL URL will be). In my example we will name it FetchAttachment.
    4. Type in a description
    5. You can choose to leave the one function named "execute" and leave it or name your function something you want. In my example we will leave it as "execute"
    6. In the Script put this:



    7. count = 1;
      var StringUtil = Packages.com.glide.util.StringUtil;

      var gr = new GlideRecord("sys_attachment");
      gr.addQuery("table_sys_id", request.sys_id);
      gr.query();

      //Starting to create the XML Document Response
      var xmldoc = new XMLDocument("<executeResponse></executeResponse>");

      while (gr.next()){
      //Getting the file and encoding it with Base 64 encoding
      var sa = new Packages.com.glide.ui.SysAttachment();
      var binData = sa.getBytes(gr);
      var encData = StringUtil.base64Encode(binData);
      //Creating the XML document elements for each attachcment
      xmldoc.createElement("file_name_" + count, gr.file_name);
      xmldoc.createElement("encodedAttachment_" + count, encData);

      count += 1;
      }
      //Sending the XML Document as the SOAP response
      response.soapResponseElement = xmldoc.getDocumentElement();


    8. As you can see, the SOAP Web Service takes a "sys_id" of the incident and will return an XML document with an entry for file_name_1, and encodedAttachment_1, then file_name_2, encodedAttachment_2 with the attachment content in an base64 encoded string.

  2. The third party that is issuing this SOAP request can then parse this response and do what they need with it.



Processor
The advantage of using a processor is that you can just go to a URL on a browser and get returned what you want in the browser

  1. Create a new Processor

    1. Click on System Definition | Processors
    2. Click on the New button
    3. Set the Name
    4. Set the Type to "Script"
    5. Set the Path parameter. (This will be what your URL will be when you call it later)
    6. In the Script put this:



    7. var incidentNumber = g_request.getParameter("incident");
      var incidentInfo = new GlideRecord("incident");
      incidentInfo.addQuery("number", "=", incidentNumber);
      incidentInfo.query();
      while (incidentInfo.next()){
      var incidentSysID = incidentInfo.sys_id;
      }

      count = 1;
      var StringUtil = Packages.com.glide.util.StringUtil;

      var gr = new GlideRecord("sys_attachment");
      gr.addQuery("table_sys_id", incidentSysID);
      gr.query();

      //Starting to create the XML Document Response
      var xmldoc = new XMLDocument("<executeResponse></executeResponse>");

      while (gr.next()){
      //Getting the file and encoding it with Base 64 encoding
      var sa = new Packages.com.glide.ui.SysAttachment();
      var binData = sa.getBytes(gr);
      var encData = StringUtil.base64Encode(binData);
      //Creating the XML document elements for each attachcment
      xmldoc.createElement("file_name_" + count, gr.file_name);
      xmldoc.createElement("encodedAttachment_" + count, encData);

      count += 1;
      }
      //Sending the XML Document as the SOAP response
      g_processor.writeOutput(xmldoc);


    8. As you can see this code takes one parameter the ?incident=incident_number. It then does a glide record lookup of that incident, gets the sys_id, then looks up the attachments for that incident in the sys_attachment table and returns them in an XML document with an entry for file_name_1, and encodedAttachment_1, then file_name_2, encodedAttachment_2 with the attachment content in an base64 encoded string.

  2. To kick this off you type in this URL: https://instanceURL/incidentAttachments.do?incident=INC0019185

6 Comments