jason_petty
Tera Expert
Options
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
01-11-2013
02:41 PM
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.
- Create a new Scripted Web Service
- Click on System Web Services | Scripted Web Services
- Click New button
- Set the Name (This will determine what the WSDL URL will be). In my example we will name it FetchAttachment.
- Type in a description
- 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"
- In the Script put this:
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();- 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.
- 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
- Create a new Processor
- Click on System Definition | Processors
- Click on the New button
- Set the Name
- Set the Type to "Script"
- Set the Path parameter. (This will be what your URL will be when you call it later)
- In the Script put this:
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);- 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.
- To kick this off you type in this URL: https://instanceURL/incidentAttachments.do?incident=INC0019185
6 Comments
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.