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.

Copy of attachment from variable to record

Tanya10
Tera Contributor

Hi,
I have a variable type attachment on my catalog form. Post submission of request I want to attach that attachment to the requested item so that it shows on the top of the ritm, user doesn't need to scroll down and see for variable attachment
This is what I have tried and it is not working as expected it does copy but every time it copied to ZZ_YYsc_req_item instead sc_req_item

 

var sysID = '72e1e6fbc30d3e5026b5652599013189'; // RITM sys_id


// Get the variables for this RITM
var itemVars = new GlideRecord('sc_item_option_mtom');
itemVars.addQuery('request_item', sysID);
itemVars.query();

while (itemVars.next()) {
    var itemOption = itemVars.sc_item_option; // Points to sc_item_option record
    var variableDef = itemOption.item_option_new; // Points to question definition

    // Check if this is your attachment variable
    if (variableDef == 'ca5cbd8cc3e3a21026b56525990131c6') {
        gs.print('Found attachment variable: ' + variableDef);

        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_name', 'ZZ_YYsc_req_item');
        attachmentGR.addQuery('table_sys_id', sysID);
        attachmentGR.query();

        while (attachmentGR.next()) {
            gs.print('Copying attachment: ' + attachmentGR.file_name);

            // Copy attachment from variable to RITM
            var newSysId = new GlideSysAttachment().copy(
                attachmentGR.table_name,
                attachmentGR.table_sys_id,
                'sc_req_item',
                sysID
            );

            gs.print('Copied attachment to RITM: ' + newSysId);
        }
    }
}
2 REPLIES 2

Saloni Suthar
Giga Sage
Giga Sage

Hi @Tanya10 ,

Please create a business rule on Requested Item table.

When = async

On Insert

 

Script:

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

	// Add your code here
	var gr = new GlideRecord("sys_attachment");
	gr.addQuery("table_name", "ZZ_YY" + current.getTableName());
	gr.addQuery("table_sys_id", current.sys_id);
	gr.query();
	while (gr.next()) {
		gr.table_name = current.getTableName();
		gr.update();
		new global.VariableUtil().copy(gr.sys_id, current.getTableName(), current.sys_id); 
	}

})(current, previous);

Please test and let me know if you run into any issues


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni

ChallaR
Giga Guru

Hi @Tanya10 ,

i have updated few lines please check if this is working -

var ritmSysId = '72e1e6fbc30d3e5026b5652599013189'; // RITM sys_id
var attachmentVariableSysId = 'ca5cbd8cc3e3a21026b56525990131c6'; // Attachment variable sys_id

// Get the variable mapping for this RITM
var itemVars = new GlideRecord('sc_item_option_mtom');
itemVars.addQuery('request_item', ritmSysId);
itemVars.query();

while (itemVars.next()) {
    var itemOption = itemVars.sc_item_option; // sc_item_option record
    var variableDef = itemOption.item_option_new; // question definition sys_id

    // Check if this is the attachment variable
    if (variableDef == attachmentVariableSysId) {
        gs.print('Found attachment variable: ' + variableDef);

        // Get attachments linked to the variable (table = ZZ_YYsc_item_option)
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_name', 'ZZ_YYsc_item_option'); // Correct table for variable attachments
        attachmentGR.addQuery('table_sys_id', itemOption); // sys_id of sc_item_option record
        attachmentGR.query();

        while (attachmentGR.next()) {
            gs.print('Copying attachment: ' + attachmentGR.file_name);

            // Copy attachment to RITM (sc_req_item)
            var newSysId = new GlideSysAttachment().copy(
                attachmentGR.getTableName(),
                attachmentGR.getValue('table_sys_id'),
                'sc_req_item',
                ritmSysId
            );

            gs.print('Copied attachment to RITM: ' + newSysId);
        }
    }
}

 

NOTE -

 

  • Source Table for Attachments
    Attachments for variable fields are stored under ZZ_YYsc_item_option (not ZZ_YYsc_req_item).

  • Source Sys ID
    Use itemOption (sys_id of sc_item_option) for table_sys_id when querying attachments.

  • Copy Logic
    Use attachmentGR.getTableName() and attachmentGR.getValue('table_sys_id') to copy from the actual attachment record.

Please mark as correct and close the thread if this is helpfull.

 

Thanks,

Rithika.ch