The CreatorCon Call for Content is officially open! Get started here.

Copy attachment from 1 record to another without using Copy function

rishabh katari1
Mega Guru

Hi Everyone,

I want to copy attachment from SC_TASK table to SC_REQUEST table. Actually I have made a UI action that attaches the attachment to sc_task table and I want that same attachment to get attached to the request record. I am able to attach the attachment using COPY method in GlideSysAttachment, but there is an issue with this as if someone clicks that UI action more than once, then it again copies all the attachments to target request record. I am using below code that's not working as per expectation:

    createAssetConditionReportReq: function(catalogTask) {
        try {
            var donorAttSysID,
                grNewAttachment,
                linkToNewRecord,
                attDataRecord,
                newDocRecord,
                grAttachment = new GlideRecord('sys_attachment');
            grAttachment.addQuery('table_name', "sc_task");
            grAttachment.addQuery('table_sys_id', catalogTask.sys_id);
            // grAttachment.addQuery('file_name', catalogTask.number + "_" + "Asset Condition.pdf");
            grAttachment.addEncodedQuery("sys_created_onONLast minute@javascript:gs.beginningOfLastMinute()@javascript:gs.endOfLastMinute()");
            grAttachment.query();
            while (grAttachment.next()) {
				gs.log("Found ","testing");
                donorAttSysID = grAttachment.getValue('sys_id');
				gs.log("Found "+donorAttSysID,"testing");
                grNewAttachment = copyRecord(grAttachment);
				gs.log("Found attach "+grNewAttachment,"testing");
                grNewAttachment.setValue('table_name', "sc_request");
                grNewAttachment.setValue('table_sys_id', catalogTask.request.sys_id);
                grNewAttachment.update();
                linkToNewRecord = gs.getProperty('glide.servlet.uri') + grNewAttachment.getLink();
				gs.log("Found link "+linkToNewRecord,"testing");
                attDataRecord = new GlideRecord('sys_attachment_doc');
                attDataRecord.addQuery('sys_attachment', donorAttSysID);
                attDataRecord.query();
                while (attDataRecord.next()) {
                    newDocRecord = copyRecord(attDataRecord);
                    newDocRecord.setValue('sys_attachment', grNewAttachment.getValue('sys_id'));
                    newDocRecord.update();
                }
            }
        } catch (e) {
            gs.log("test error: " + e, "test");
        }
    },

    copyRecord: function(record) {
        var i,
            recordElement,
            recordElementName,
            recordTable = record.getTableName(),
            recordFields = record.getFields(),
            grNewRecord = new GlideRecord(recordTable);

        grNewRecord.initialize();

        for (i = 0; i < recordFields.size(); i++) {
            recordElement = recordFields.get(i);
            if (recordElement.getName() != 'sys_id' && recordElement.getName() != 'number') {
                recordElementName = recordElement.getName();
                grNewRecord.setValue(recordElementName, record.getValue(recordElementName));
            }
        }

        grNewRecord.insert();
		return grNewRecord;
}

 

If anyone knows other method for doing this, please help.

Thanks in Advance

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Hi Rishabh,

 

Instead of copying why not create a relation and display it as Related list.

If there is a still a need to copy then you can try adding one more clause to check i.e. with file name

Hi @Jaspal Singh,

I tried adding that too but its not copying content of the pdf. It is able to attach empty pdf.

 

Hi @Harish Bainsla,

 

Actually by using copy method, we are having a small problem that when we run that API second time then the number of attachment records that are inserted second time are more than actual number of attachments.