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

Hi Lakshmi,

 

Were you able to come up with a solution for questions 1&2? If so would you be willing to share that solution? I'm working on a similar issue, any information would be greatly appreciated.

Thank you

-pm

To solve questions #1 and #2:

 

#1, you will need to track attachment sys_id's between systems during transmission (instance A sends attachment to instance B, and instance A retrieves attachment from instance B) and how the sys_id's map, most likely in a new table. This will allow you to know what attachment to delete from the target instance when an attachment has been deleted from your source instance.

#2, to send attachments or updates of attachments (like a rename of the attachment) you will need to consult your sys_id attachment mapping table (see #1 answer above). This will inform your logic on which path to pursue: Create new attachment in target instance, or update existing attachment in target instance.

 

Danny

Hi Moonep,

 

I was able to resolve 2 by creating a Business rule on sys_atatchment table for 

table name incident and pass table name,targetid and filename sysid as below

 

function fetchTargetIncident() {

var inc = new GlideRecord("incident");
inc.addActiveQuery();
inc.addQuery('sys_id',current.table_sys_id);
inc.query();
if(inc.next()) {
// Upload attchments in Target Instance
sendAttach('incident',current.table_sys_id,inc.targetincidentnumber);
}
}


function sendAttach(sourceTable, sourceID, targetID) {
var attachmentMsg = new sn_ws.RESTMessageV2('Upload Attachments', 'Attachments');
attachmentMsg .setQueryParameter("table_name","incident");
attachmentMsg .setQueryParameter("table_sys_id", targetID);
attachmentMsg .setQueryParameter("file_name", current.file_name);
attachmentMsg .setRequestBodyFromAttachment(current.sys_id);

var response = attachmentMsg .execute();
var responseBody = response.getBody();

Regards,

Lakshmi Prabha Elangovan

This is perfect thank you for posting. 

@michael.ritchie  I tried the above accepted solution and i'm getting "Unexpected behavior from remote host: Unbuffered entity enclosing request can not be repeated."   any thoughts? 

 

is the content from the k17 lab you referred to posted and if so what was the name of the class/lab?