Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Alternate way

Prasun Sarkar7
Tera Expert

I am in a domain separated instance and this 

var copyAtt = new GlideSysAttachment();

copyAtt.copy(sourceTable, sourceSysId, targetTable, targetSysId);

this thing doesnt work in domain separted instance is there any other way to use gliderecord to achieve what this funtion does?

1 REPLY 1

k_lutz
Tera Guru

There may be several ways to do it. The following is not tested but may be a starting point to not use the .copy() which may be failing.

 

function copyAttachments(sourceTable, sourceSysId, targetTable, targetSysId) {
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', sourceTable);
grAttachment.addQuery('table_sys_id', sourceSysId);
grAttachment.query();

while (grAttachment.next()) {
// Get the attachment data
var sa = new GlideSysAttachment();
var attachmentBytes = sa.getBytes(grAttachment);

// Create new attachment for target record
var newAttachment = new GlideRecord('sys_attachment');
newAttachment.initialize();
newAttachment.table_name = targetTable;
newAttachment.table_sys_id = targetSysId;
newAttachment.file_name = grAttachment.file_name;
newAttachment.content_type = grAttachment.content_type;
var newAttachmentId = newAttachment.insert();

// Write the bytes to the new attachment
sa.writeBase64(newAttachment, attachmentBytes);
}
}

// Usage
copyAttachments(sourceTable, sourceSysId, targetTable, targetSysId);