Remove prefix from table name in sys_attachment table

Nishant26
Tera Contributor

Hi @Ankur Bawiskar ,

 

I need to update the attachment from record producer to the incident table form. I am seeing "ZZ_YY" prefix in the table name field on the sys_attachment" table. this is causing the error while executing the code. 

Here is the code: 

 

var gr = new GlideRecord('sys_attachment');
if(gr.get(producer.number_list)){ //attachment field name
    gr.table_name='incident';     //copy to table name
    gr.table_sys_id=current.sys_id;//copy to record sys_id
    gr.update();
    
}
 
I need to remove the prefix "ZZ_YY" from the table name field in the sys_attachment table. how can this be achieved.
 
Thanks!
1 ACCEPTED SOLUTION

@Nishant26 

can you try this code?

I have commented this line

// new global.VariableUtil().copyAttachment(gr.sys_id, current.getTableName(), current.sys_id);

(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);

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

16 REPLIES 16

I forgot to mention, the target table(u_abc) is in a scoped application and I have created the BR in the same scope.

Requirement is to copy the attachment from the record producer's attachment variable to the target table(u_abc) record once the record producer form is submitted. The attachment should be visible on the header of the target table's record.

I have used the same script in an After insert BR as it has been mentioned by you, but it is not working.

 

@Nishant26 

The same logic has worked earlier so it should work fine now as well.

Did you create Async insert BR?

Please share your script and BR configuration as well.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Yes, BR is async insert and here is the code:

 

(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);

@Nishant26 

Did you debug BR and see if BR triggered?

If the IF condition got satisfied or not

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

BR is working and attachment is also getting copied on the Record form but at the same time, it is creating two records in the sys_attachment table for the same u_abc table record, one with "ZZ_YY" prefix and one without the prefix.