Best Practice for Attachments and Updating Records in Record Producer

Mike S3
Giga Expert

Overview : We are using a record producer to create a new record or update a record on a table based on what criteria the user specifies . 

The process works as intended when users are creating new records and upload attachments . New records are created and those attachments are linked correctly .

Issue: We are having an issue with updating records and saving attachments. I've noticed that as soon as you upload an attachment , a table_sys_id is generated in the sys _atttachment table . However since we're updating a record , we already have a sys id that should be used . 

I have a script that I have placed in the script section of the record producer. While it works ,its not ideal since it checks for attachments recently uploaded by the user within the past minute and copies that attachment to the desired sys id .  In production , users may take a long time to upload attachments and submit the record producer so this wouldn't be the best option . 

Does anyone have any better solutions we can use ? 

var user = gs.getUserName();  // to get the user's login id
var str = 'sys_created_onONCurrent minute@javascript:gs.beginningOfCurrentMinute()@javascript:gs.endOfCurrentMinute()';  // query to check created in last minute

 
var att = new GlideRecord('sys_attachment');
att.addQuery('sys_created_by', user);
att.addQuery('table_name','u_earb');
att.addEncodedQuery(str);
att.query();
if (att.next())
{
 GlideSysAttachment.copy('u_earb',att.table_sys_id,'u_earb',record);
att.update();
}
1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron
Tera Patron

Hi

unfortunately ServiceNow will not allow you the attachment content to be overridden. You need to attach a fresh file and possibly delete the older one.

The reason is that ServiceNow is not a document management system as all file data is stored in a separate attachments table.

Kind regards
Maik

View solution in original post

3 REPLIES 3

Maik Skoddow
Tera Patron
Tera Patron

Hi

unfortunately ServiceNow will not allow you the attachment content to be overridden. You need to attach a fresh file and possibly delete the older one.

The reason is that ServiceNow is not a document management system as all file data is stored in a separate attachments table.

Kind regards
Maik

Thanks for the quick response Mark . Do you see any possible improvements in my script logic ? Ideally, I don't want to query the attachment based on if it was created within the past minute. But I can't figure out another way to query the correct file.

Hi

what about taking the "Hash" value from sys_attachment table to lookup existing attachment records with the same hash value?

see https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0830770

Kind regards
Maik