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.

Why are uploaded files lost after order guide are sent?

Dat Phan
Tera Guru

I created the "Demo multi Row 001"multirow variable which has a field to reference the "sys_attachment".

When user upload file by the "Demo Attachment" attachment field. I cloned it. And I added it into "Demo Multi Row 001". But when the order guide is sumitted, the files in the something is deleted. What's the happend?

Screenshot 2023-12-30 110753.png

I used the on_change event for the "Demo Attachment" attachment field:

 

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var attachmentAjax = new GlideAjax("DemoAttachmentUtil");
    attachmentAjax.addParam('sysparm_name', 'cloneAttachmentByAjax');

    attachmentAjax.addParam('sysparm_attachmentId', newValue);

    attachmentAjax.getXMLAnswer(function(response) {

        var newAttachmentId = "";
        if (response) {
            var resultData = JSON.parse(response);
            newAttachmentId = resultData.attachmentId;


            var jsonData = g_form.getValue('demo_multi_row_001');

            var lstFiles = [];
            if (jsonData) {

                lstFiles = JSON.parse(jsonData);
            }
            lstFiles.push({
                file_attachment: newAttachmentId
            });
            g_form.setValue('demo_multi_row_001', JSON.stringify(lstFiles));
        }
    });
}

 

 

 

, and script include "DemoAttachmentUtil":

 

 

var DemoAttachmentUtil = Class.create();
DemoAttachmentUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    cloneAttachmentByAjax: function() {
		var result = {
			status: 0,
			attachmentId: ''
		};
		try {
			var paramAttachmentId = this.getParameter('sysparm_attachmentId');
			result.attachmentId = this.copySingleAttachment(paramAttachmentId);
			result.status = 1;
		} catch(ex) {
			gs.info("TEST :" + ex);
			result.attachmentId = '';
		}
		return JSON.stringify(result);
	},
    copySingleAttachment: function(attachmentFileId) {
        var attRec;
        var orgAttachment = new GlideRecord('sys_attachment');
        orgAttachment.addQuery('sys_id', attachmentFileId);
        orgAttachment.query();
        if (orgAttachment.next()) {
            var cloneAttachment = new GlideRecord('sys_attachment');
            cloneAttachment.initialize();
            cloneAttachment.file_name = orgAttachment.file_name;
            cloneAttachment.content_type = orgAttachment.content_type;
            cloneAttachment.compressed = orgAttachment.compressed;
            cloneAttachment.table_name = orgAttachment.table_name;
            cloneAttachment.size_bytes = orgAttachment.size_bytes;
            cloneAttachment.size_compressed = orgAttachment.size_compressed;
            cloneAttachment.table_sys_id = orgAttachment.table_sys_id;
            attRec = cloneAttachment.insert();
        }

        var attDoc = new GlideRecord('sys_attachment_doc');
        attDoc.addQuery('sys_attachment', orgAttachment.sys_id);
        attDoc.query();
        while (attDoc.next()) {
            var attDocCopy = new GlideRecord('sys_attachment_doc');
            attDocCopy.initialize();
            attDocCopy.sys_attachment = attRec;
            attDocCopy.position = attDoc.position;
            attDocCopy.length = attDoc.length;
            attDocCopy.data = attDoc.data;
            attDocCopy.insert();
        }
        return attRec;
    },
    isPublish: function() {
        return true;
    },
    type: 'DemoAttachmentUtil'
});

 

 

 

0 REPLIES 0