Disable ZZ_YY prefix from attachment For Multiple Attachment Variables

kshaw
Giga Guru

@Ankur Bawiskar 

Your solution to the original problem worked well when I added the business rule.

However, in my catalog item I have 2 attachment variables. Your scripted BR is only picking up the attachment from one of the attachment variables.

How can the script be changed to look for both (all) attachment variables on the current RITM record?

 

Your help will be creatly appreciated.

1 ACCEPTED SOLUTION

kshaw
Giga Guru

I modified the script after seeing another comment from you about the OOB syntax change.

New code is

(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();
	if (gr.next()) {
		gr.table_name = current.getTableName();
		gr.update();
		new global.VariableUtil().copyAttachment(gr.sys_id, current.getTableName(), current.sys_id); 
	}

})(current, previous);

After running a new test, this time the script saw 2 attachment variables BUT it only copied one file twice to the sys_attachment table.

2 attachments.png

 

the resulting sys_attachments showing in the sub-header - both files the same

resulting attachments.png

 

 

View solution in original post

3 REPLIES 3

kshaw
Giga Guru

I modified the script after seeing another comment from you about the OOB syntax change.

New code is

(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();
	if (gr.next()) {
		gr.table_name = current.getTableName();
		gr.update();
		new global.VariableUtil().copyAttachment(gr.sys_id, current.getTableName(), current.sys_id); 
	}

})(current, previous);

After running a new test, this time the script saw 2 attachment variables BUT it only copied one file twice to the sys_attachment table.

2 attachments.png

 

the resulting sys_attachments showing in the sub-header - both files the same

resulting attachments.png

 

 

 

Hi @kshaw 

 

Please try the below script and confirm if it picks up all the attachments from variable.

 

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

    var atch = new GlideRecord("sys_attachment");
    atch.addQuery("table_name", "ZZ_YY" + current.getTableName());
    atch.addQuery("table_sys_id", current.sys_id);
    atch.query();

    while (atch.next()) { // Loop through all found attachments
        var oldSysId = atch.sys_id;  // Store the old sys_id before updating

        atch.table_name = current.getTableName();
        atch.update();
        new global.VariableUtil().copyAttachment(oldSysId, current.getTableName(), current.sys_id);
    }

})(current, previous);

Regards,

Sathish Kumar

sayalisheth
Tera Contributor

I am not an expert, but it may be because you are using if instead of while? so it's only picking up the first one. Just a guess. 

 

I am trying this in my instance with one variable and the BR doesn't seem to work. Can you let me know where you wrote the BR and if it was set to Before/After/Async and on Insert or Update?

 

Thank You!