Custom Attachment Field Removing OOB Attachments in ServiceNow Portal Form

Krishnakant Arv
Tera Expert

Hi Developers,

Hope you are doing great!!

I am using two attachment fields in my ServiceNow Portal Form:

  • A custom attachment field
  • The OOB "Add Attachments" field.

The issue occurs when I attach a new template in the custom field, which results in any attachment in the OOB attachment field being removed or deleted. This behavior is not expected, and I need a solution where both attachment fields can function independently, allowing attachments to be uploaded and retained in both fields simultaneously.

Desired Outcome:

  • Ensure that attachments added in the custom "Add Creator Attachment" field do not interfere with or remove attachments from the OOB "Add Attachments" field.
  • Both fields should be able to retain their respective attachments.

Has anyone else encountered this issue? If so, how did you resolve it? Any guidance or solutions would be great help.

 

Thanks,

Krishnakant Yadav

 

Analysis 

1 ACCEPTED SOLUTION

Krishnakant Arv
Tera Expert

Hi Developers, 

 

After multiple analysis and many attempts in Widget Script, Onchange Catalog client script, checking BRs, Inspecting portal forms at last found out that this is happenning because of Onchange of Add Creator Attachment" Catalog Client Script. After furthur investigation found out that we are calling one Script Include that is for File name validation on Add Creator Attachment. Additionaly few months back we have noticed one more issue for add creator attachment that if any attchment is already attched previously and we try to upload one more file without deleting the existing attachment then in attachmnet table two records were there for same table sys id which is not expected so we wriiten one more script include to delete that existing attachment record sys id. 

Code -  

duplicateAttachment: function(attach_sys_id) {
        var grp = [];
        var gratt1 = new GlideRecord('sys_attachment');
        if (gratt1.get(attach_sys_id)) {
            var gratt = new GlideRecord('sys_attachment');
            gratt.addEncodedQuery('sys_id!=' + attach_sys_id + '^table_sys_id=' + gratt1.table_sys_id );
            gratt.query();
            while (gratt.next()) {
                grp.push(gratt.sys_id);
                gratt.deleteRecord();
            }
        }
    }
 
After logging multiple logs and checking sys id in attachment table found that this function  is removing/deleting attachment which was attached for OOB Add Creator attachment because those two custom & OOB attachment was there in form and if we are creating any request by adding attachment in both fields then we can notice that same Table Sys id will be there in attachment table that's why this above function was deleting attchment from OOB  attachment field.
 
How I have resolved this issue - 
So I notice that if we are attaching any attchment in custom attachment field then table name in attachment table for that record will be ZZ_YYsc_cart_item because we are creating request with help of record Producer and we are not yet submitted that request and it we attach any attachment to OOB attachment field then table name in attachment table for that record will be your table name where this request will be created after submission of request. So I found out this difference in table name of both records in attachment table so I modified this functon by adding one more query to encoded query i.e table_nameNOT LIKEZZ_YY. 
 
Updated function - 
 
duplicateAttachment: function(attach_sys_id) {
        var grp = [];
        var gratt1 = new GlideRecord('sys_attachment');
        if (gratt1.get(attach_sys_id)) {
            var gratt = new GlideRecord('sys_attachment');
            gratt.addEncodedQuery('sys_id!=' + attach_sys_id + '^table_sys_id=' + gratt1.table_sys_id  + '^table_nameNOT LIKEZZ_YY');
            gratt.query();
            while (gratt.next()) {
                grp.push(gratt.sys_id);
                gratt.deleteRecord();
            }
        }
    }
 
Additional Point :- If you are creating your request using record producer and you attached attachment in custom attachment field then table name for that record in attachment table will be ZZ_YYsc_cart_item but once record is submitted then table name will be ZZ_YY followed by your table name where that request is created along with attachment for example ZZ_YYincident.
 
Hope it will help some one in future.
 
Thanks,
Krishnakant Yadav

View solution in original post

1 REPLY 1

Krishnakant Arv
Tera Expert

Hi Developers, 

 

After multiple analysis and many attempts in Widget Script, Onchange Catalog client script, checking BRs, Inspecting portal forms at last found out that this is happenning because of Onchange of Add Creator Attachment" Catalog Client Script. After furthur investigation found out that we are calling one Script Include that is for File name validation on Add Creator Attachment. Additionaly few months back we have noticed one more issue for add creator attachment that if any attchment is already attched previously and we try to upload one more file without deleting the existing attachment then in attachmnet table two records were there for same table sys id which is not expected so we wriiten one more script include to delete that existing attachment record sys id. 

Code -  

duplicateAttachment: function(attach_sys_id) {
        var grp = [];
        var gratt1 = new GlideRecord('sys_attachment');
        if (gratt1.get(attach_sys_id)) {
            var gratt = new GlideRecord('sys_attachment');
            gratt.addEncodedQuery('sys_id!=' + attach_sys_id + '^table_sys_id=' + gratt1.table_sys_id );
            gratt.query();
            while (gratt.next()) {
                grp.push(gratt.sys_id);
                gratt.deleteRecord();
            }
        }
    }
 
After logging multiple logs and checking sys id in attachment table found that this function  is removing/deleting attachment which was attached for OOB Add Creator attachment because those two custom & OOB attachment was there in form and if we are creating any request by adding attachment in both fields then we can notice that same Table Sys id will be there in attachment table that's why this above function was deleting attchment from OOB  attachment field.
 
How I have resolved this issue - 
So I notice that if we are attaching any attchment in custom attachment field then table name in attachment table for that record will be ZZ_YYsc_cart_item because we are creating request with help of record Producer and we are not yet submitted that request and it we attach any attachment to OOB attachment field then table name in attachment table for that record will be your table name where this request will be created after submission of request. So I found out this difference in table name of both records in attachment table so I modified this functon by adding one more query to encoded query i.e table_nameNOT LIKEZZ_YY. 
 
Updated function - 
 
duplicateAttachment: function(attach_sys_id) {
        var grp = [];
        var gratt1 = new GlideRecord('sys_attachment');
        if (gratt1.get(attach_sys_id)) {
            var gratt = new GlideRecord('sys_attachment');
            gratt.addEncodedQuery('sys_id!=' + attach_sys_id + '^table_sys_id=' + gratt1.table_sys_id  + '^table_nameNOT LIKEZZ_YY');
            gratt.query();
            while (gratt.next()) {
                grp.push(gratt.sys_id);
                gratt.deleteRecord();
            }
        }
    }
 
Additional Point :- If you are creating your request using record producer and you attached attachment in custom attachment field then table name for that record in attachment table will be ZZ_YYsc_cart_item but once record is submitted then table name will be ZZ_YY followed by your table name where that request is created along with attachment for example ZZ_YYincident.
 
Hope it will help some one in future.
 
Thanks,
Krishnakant Yadav