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

Please help me further on this.I am trying to send such attachments in encrypted format by using the following line of code in the above sccript


attachmentMessage.setQueryParameter("encryption_context", '8bd17d9fdb7a72000ebef20ebf961952');


I have created an encryption context and associated it with a role and using the sys_id of same encryption context   in the line above.My issue is that when i am receiving the attachment in another instance it is no longer encrypted.It is coming in the same manner as it was coming when no encryption context was defined.Please let me know if i am missing something


Unfortunately I have no experience with encrypting attachments so I am unable to help.   You may want to create another post specifically asking about this to get better visibility.


I have created one.Thanks!!!!


kris9971
Tera Contributor

It worked beautifully !!

Hey Kris,

Can you help me in same issue.

Business Rule:

(function executeRule(current, previous /*null when async*/) {

    repeatAttach();
    function repeatAttach(){
        var grAttach= new GlideRecord('sys_attachment');
        grAttach.addQuery('table_name','IN','incident');
        grAttach.orderByDesc('file_name');
        grAttach.setLimit(1);
        grAttach.query();
        while(grAttach.next())
            {
                var attachmentID=grAttach.sys_id;
                var table_name=grAttach.table_name;
                var table_sys_id=grAttach.table_sys_id;
                var file_name=grAttach.file_name;
                var content_type=grAttach.content_type;
                
                sendAttachment(attachmentID,table_name,table_sys_id,file_name,content_type);
                
            }
    }
    
function sendAttachment(attachmentID,table_name,table_sys_id,file_name,content_type)
    {
try { 
 var r = new sn_ws.RESTMessageV2('Add Attachment', 'POST attachment');
 r.setStringParameterNoEscape('table_name', table_name);
 r.setStringParameterNoEscape('file_name', file_name);
 r.setStringParameterNoEscape('content_type', content_type);
 r.setStringParameterNoEscape('table_sys_id', table_sys_id);
 r.setRequestBodyFromAttachment(attachmentID);
r.setRequestHeader("Content_Type",content_type);
    

 var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
    gs.info("Status:" +httpStatus+ ' - '+ responseBody);
}
catch(ex) {
 var message = ex.getMessage();
}

}
})(current, previous);