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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

What type of variable is producer.number_list? If I look at your description, this is a string? When performing a .get in the way you are doing now, it should be a sys_id instead.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mohan raj
Mega Sage

Hi @Nishant26,

Try this script, 

var gr = new GlideRecord('sys_attachment');
if(gr.get("producer.number_list")){ //attachment field name

    gr.table_name= gr.table_name.replace('ZZ_YY','');     
    gr.table_sys_id=current.sys_id;
    gr.update();   
}

 

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

Regards,

T Mohan.

I have tried the script but still not getting the result, I am unable to copy the attachment from the record producer's attachment variable to Incident table record on the which the record producer is created.

Ankur Bawiskar
Tera Patron
Tera Patron

@Nishant26 

what is your business requirement?

If you are having a attachment type variable then it shows ZZ_YY prefix when you attach file to that variable. It's platform behavior.

Are you saying you want to copy that file to the record which gets generated from record producer?

I shared solution few years ago for this; refer this link

Disable ZZ_YY prefix from attachment when added through record producer 

Sharing script here as well

1) I created Async After Insert BR on the target table

2) Script as this

a) Remove the ZZ_YY

b) Then use the Copy Attachment

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

Output:

1) The file added to the record

AnkurBawiskar_0-1690860311025.png

 

2) The file present on the attachment variable

AnkurBawiskar_1-1690860311063.png

 

 

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