Importing Attachments from one instance to another from a record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2023 08:30 AM
Hello community
I am trying to import the knowledge articles along with attachment(s) from one instance to another.
1. I have an excel file with KB information(along with kb sys_id) from instance A.
2. Created an import set and transform map to pull the KB information
3. Created an onAfter transform script to pull the attachments.
a. Able to get the attachments metadata using GET Attachment.
b. Able to loop through the each attachment(if there are mutliple) and call GET Attachment File
c. With the response from step 3.b, I used request.saveResponseBodyAsAttachment(tablename, kbID, fileName) which saved the record.
The issue is if the source kb record has only one attachment, the above solution is working fine. If there is more than one attachment, the loop(or iteration) is failing as we reach to second attachment.
Instead of Step 3.b, I used GlideAttachment.write function for the responseBody that I received from Step-3.b and able to attach the attachments to target record but the files are broken. I tried both binary file and encoded64baseString (GlideStringUtil.base64Encode(attachmentResponseBody)) and both gave broken files.
Also tried POST Call from Step 3.b and even it attached only one single file and loop broken.
Appreciate if someone could help me in figuring out how to import multiple attachments associated with a source record from one instance and attach them to target record in another instance.
Attaching my code here
var tablename = 'kb_knowledge'; // Source Table Name
var sourceSysID = '1863aedf97a031107b7c7f200153af9e';//source.u_sys_id; // Source KB sysID
var newAttachmentSysID;
var kbID = '1e900cff47a03110a3d50592e36d433a';//target.sys_id; // Target KB sys ID
getSourceAttachmentInfo(sourceSysID);
function getSourceAttachmentInfo(sourceSysID){
try {
var r = new sn_ws.RESTMessageV2('KB Attachment', 'Get Attachment SysID'); // Retriving metadata of attachment(s)
r.setStringParameterNoEscape('kb_sys_id', sourceSysID);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var results = JSON.parse(responseBody);
gs.log(httpStatus+'httpStatus');
gs.log(results.result.toString()+'results transform map code1');
//var attachements = JSON.stringify(JSON.parse(responseBody));
var attachments = results.result;
gs.log('attachments lenght'+attachments.length);
for(i=0;i<attachments.length;i++){
gs.log('count'+i);
var fileName = attachments[i].file_name.toString();
var sourceAttachmentSysID = attachments[i].sys_id.toString();
var sourceTableName = attachments[i].table_name.toString();
var contentType = attachments[i].content_type.toString();
var link = attachments[i].download_link;
saveAttachment(tablename,kbID,fileName,sourceAttachmentSysID,contentType);
}
}
catch(ex) {
var message = ex.message;
gs.print('error Message'+message);
}
}
function saveAttachment(tablename,kbID,fileName,sourceAttachmentSysID,contentType,link){
try{
var r = new sn_ws.RESTMessageV2('KB Attachment', 'GET Attachment');
r.setStringParameterNoEscape('attachment_id', sourceAttachmentSysID);
r.saveResponseBodyAsAttachment(tablename, kbID, fileName);
var attachmentResponse = r.execute();
var attachmentResponseBody = attachmentResponse.getBody();
var base64String = GlideStringUtil.base64Encode(attachmentResponseBody);
gs.print(base64String + 'base64String');
var attachmentResponseHttpStatus = attachmentResponse.getStatusCode();
gs.log(attachmentResponseHttpStatus+'attachmentResponseHttpStatus transform map code1');
}
catch(ex) {
var message = ex.message;
gs.print('error Message12'+message);
}
/*
## Used this lines of code instead of r.saveResponseBodyAsAttachment(tablename, kbID, fileName); but attached broken attachments ##
var kb = new GlideRecord('kb_knowledge');
kb.get(kbID);
attachment.write(kb,fileName,contentType,base64String);
*/
/*
### used this below lines of code instead of r.saveResponseBodyAsAttachment(tablename, kbID, fileName); but did not attach all the attachment ##
var postRequest = new sn_ws.RESTMessageV2('KB Attachment', 'POST to current instance');
postRequest.setStringParameterNoEscape('recSysID', kbID); // KB SysID
postRequest.setStringParameterNoEscape('table', tablename); //table
postRequest.setStringParameterNoEscape('content-type', contentType); // content type
postRequest.setStringParameterNoEscape('file_name', fileName); // file name
postRequest.setRequestBody(attachmentResponseBody);
var postresponse = postRequest.execute();
var postresponseBody = response.getBody();
var posthttpStatus = response.getStatusCode();
*/
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-16-2023 12:52 PM
Can you not use a simple XML Export and then Import into the other instance? It will include ALL the attachments.
The biggest issue with that method would be the KB Article number coming from the source instance, but they could be updated if required.