Copying attachment from one instance to another+

khusboo1
Kilo Expert

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.

1 ACCEPTED SOLUTION

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;


}


View solution in original post

25 REPLIES 25

Hans Hoess
Tera Contributor
It works great, perfect !!!