- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2017 05:16 AM
I am getting the following error while copying attachments from one instance to another in helsinki instance using SOAP.The error is
'Illegal attempt to access class 'com.glide.util' via script'.Any help is deeply appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2017 06:54 AM
Lets try another way to help you... At K17 a few weeks ago I had created a HackLab on this topic and unfortunately that lab guide hasn't been posted yet to the Community. In that lab there was a use case where an incident needs to be sent from one ServiceNow instance to another and along with the record's attachments. Basically a business rule runs after insert of an incident based on whatever conditions you need it to. Below is the script that first send the incident to the target and then query for any attachments and send those as well. Hopefully this example script will be helpful to solve your issue.
var targetInstanceURL = "https://TARGET-NAME.service-now.com/";
var targetUserID = "USER-NAME";
var targetUserPassword = "USER-PASSWORD";
var answer = "";
var attachmentMsg = "";
//Create a JSON string with the current incident attributes
var incidentJSON = {};
incidentJSON.caller_id = current.caller_id.toString();
incidentJSON.category = current.category.toString();
incidentJSON.impact = current.impact.toString();
incidentJSON.urgency = current.urgency.toString();
incidentJSON.priority = current.priority.toString();
incidentJSON.short_description = current.short_description.toString();
// Set the Correlation fields to link the new incident with the source incident
incidentJSON.correlation_display = "ServiceNow";
incidentJSON.correlation_id = current.sys_id.toString();
// Encode JSON string
var requestBody = new global.JSON().encode(incidentJSON);
// Create incident in target instance via REST
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setHttpMethod("post");
restMessage.setBasicAuth(targetUserID, targetUserPassword);
restMessage.setEndpoint(targetInstanceURL + "api/now/table/incident");
restMessage.setRequestHeader("Content-Type", "application/json");
restMessage.setRequestBody(requestBody);
var response = restMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
if (httpStatus.toString() == "201") {
answer = "Incident successfully sent.";
// Get target record's SysID
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
var targetRec = parsed.result;
var attachmentCount = sendAttachments(current.getTableName(), current.sys_id, targetRec.sys_id);
if (attachmentCount != "none") {
attachmentMsg = " Attachments successfully sent: " + attachmentCount[0].toString() + ". Attachments failed to be sent: " + attachmentCount[1].toString();
} else {
attachmentMsg = " Record had no attachments to send.";
}
} else {
answer = "Incident could not be sent.";
}
answer = answer + attachmentMsg;
// Set message at top of screen with results.
gs.addInfoMessage(answer);
function sendAttachments(sourceTable, sourceID, targetID) {
var answer = [0, 0]; //successful attachments, failed attachments
// Query for any attachments on the current record.
var attachmentRec = new GlideRecord("sys_attachment");
attachmentRec.addQuery("table_sys_id", sourceID);
attachmentRec.addQuery("table_name", sourceTable);
attachmentRec.query();
if (attachmentRec.hasNext()) {
while (attachmentRec.next()) {
var attachmentMessage = new sn_ws.RESTMessageV2();
attachmentMessage.setHttpMethod("post");
attachmentMessage.setBasicAuth(targetUserID, targetUserPassword);
attachmentMessage.setEndpoint(targetInstanceURL + "api/now/attachment/file");
attachmentMessage.setQueryParameter("table_name", attachmentRec.table_name);
attachmentMessage.setQueryParameter("table_sys_id", targetID);
attachmentMessage.setQueryParameter("file_name", attachmentRec.file_name);
attachmentMessage.setRequestHeader("Content-Type", attachmentRec.content_type);
attachmentMessage.setRequestHeader("Accept", "application/json");
attachmentMessage.setRequestBodyFromAttachment(attachmentRec.sys_id);
var response = attachmentMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
if (httpStatus.toString() == "201") {
answer[0] += 1;
} else {
answer[1] += 1;
}
}
} else {
answer = "none";
}
return answer;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2017 05:18 AM
Hi Khusboo,
While I don't know what could be producing that error or what it means, have you tried using the REST Attachment API? I find it easier to use than SOAP.
Getting Started with REST - ServiceNow Wiki
REST API Explorer - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2017 05:28 AM
(function sampleRESTMessageV2() {
try{
var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('get');
var attachment_sys_id = 'b45f80356f102100758ecb512e3ee485',
tablename = 'incident',
recordSysId = '05dac04edbf2320078727befbf96195d',
response,
httpResponseStatus,
filename ='sap_login.jpg';
//endpoint - ServiceNow REST Attachment API
request.setEndpoint('https://devxxxxx.service-now.com/api/now/attachment/' + attachment_sys_id +'/file');
request.setBasicAuth('admin', 'xxxxx');
//RESTMessageV2 - saveResponseBodyAsAttachment(String tableName, String recordSysId, String fileName)
//request.saveResponseBodyAsAttachment(tablename, recordSysId, filename);
response = request.execute();
httpResponseStatus = response.getStatusCode();
gs.log("http response status_code:" +httpResponseStatus);
var responseBody = response.getBody();
request.saveResponseBodyAsAttachment(tablename, recordSysId, filename);
gs.log("heello"+responseBody);
}
catch(ex){
var message = ex.getMessage();
gs.log(message);
}
})();
earlier I was using the above script,but the response body was not saved as attachment,whenever i tried the script for another instance(not the instance in which script is written), however it was working well on same instance(end point is same instance).Please help me on this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2017 05:47 AM
Please see the online documentation for posting attachments to a ServiceNow instance:
Attachment API - POST /now/attachment/file
You are missing quite a few parameters. You also have to know the SysID of the target record in the other instance to link the attachment to. I would expect the target incident to have a different SysID than your source instance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2017 06:27 AM
I have used all the three required parameters i.e file_name (Required),table_name (Required),table_sys_id (Required).The fourth one is encryption_context,which is not required.My code is working properly if endpoint is same as the instance in which script is written but if i m using a different url(i.e a different instance as the end point),response is printed in log message,but it is not getting saved as an attachment.Hope this helps.