How can I copy attachments from RITM variables to the record itself?

Guilherme Liba1
Kilo Contributor

Hello everyone, I have a requirement where I need to copy the attachments from the variables on my RITM to the record itself. I have already looked at the sys_attachment table, but I couldn't find a table with these table names'.

I also tried to search for the sys_id, without success either.

Is it possible to be done?

find_real_file.png

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

You can create an async Insert Business Rule on the sc_req_item table.  If you want the attachment(s) to appear in the header instead of the activity stream, use this script.

(function executeRule(current, previous /*null when async*/) {
	var attach = new GlideRecord('sys_attachment');
	attach.addQuery('table_name', 'ZZ_YY' + current.getTableName());
	attach.addQuery('table_sys_id', current.sys_id);
	attach.query();
	while(attach.next()){
		attach.table_name = current.getTableName();
		attach.update();
	}
})(current, previous);

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

You can create an async Insert Business Rule on the sc_req_item table.  If you want the attachment(s) to appear in the header instead of the activity stream, use this script.

(function executeRule(current, previous /*null when async*/) {
	var attach = new GlideRecord('sys_attachment');
	attach.addQuery('table_name', 'ZZ_YY' + current.getTableName());
	attach.addQuery('table_sys_id', current.sys_id);
	attach.query();
	while(attach.next()){
		attach.table_name = current.getTableName();
		attach.update();
	}
})(current, previous);

mabeto97
Tera Contributor

Also you can do it with the exact attachments uploaded to the variables. The other type may include attachments being cleared from the value based on UI Policy or Catalog Client script.

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

    var attachmentsSysIDs = '';
    var gr = new GlideRecord('sc_item_option_mtom');
    gr.addEncodedQuery('request_item=' + current.sys_id + '^sc_item_option.item_option_new.type=33');
    gr.query();
    while (gr.next()) {
        attachmentsSysIDs += gr.sc_item_option.value + ",";
    }

    var attach = new GlideRecord('sys_attachment');
    attach.addQuery('sys_id', 'IN', attachmentsSysIDs);
    attach.query();
    while (attach.next()) {
        attach.table_name = current.getTableName();
        attach.update();
    }

})(current, previous);

panda_smruti
Tera Contributor

Becareful using the above script. If you are doing 

attach.update();

 Make sure you handle when user deletes the attachment. Other wise the RITM and TASK form will not render anymore. You need to handle the MTOM update for that attachment variable.