Displaying attachments on list view

LRobinson
Giga Contributor

I have added the "has attachment" field to my Incident form, and created the BR (from the wiki) to update the field to "true" when an attachment is added to an incident.   Although it works fine when I add an attachment to an Incident, I am having two problems with the script:

1. If I delete (remove) the attachment, the field doesn't change to "false" (unchecked).

2. I want all incidents with attachments to show "true" so that we can sort on this field, and it currently is only working when I add the attachment and update.

 

I've tried multiple modifications (with weak javascript skills) and have had no luck.   This is the BR on the sys_attachment table, run "after" insert or delete:

 

checkAttachment();

 

function checkAttachment(){

      // if inserting then the task has an attachment

      if (current.operation() == 'insert' || current.hasAttachments()) {

              hasAttachment('true');

      }

 

      // if deleting attachment check for other attachments

        if (current.operation() == 'delete') {

              //var timeNow3 = new GlideDateTime();

                  //gs.log('has_attachment br: gliderecord query start date time is: ' + //timeNow3.getNumericValue(),'jwtest');

              var attachCount = new GlideAggregate('sys_attachment');

              attachCount.addQuery('table_sys_id',current.sys_id);

              attachCount.addAggregate('COUNT');

              attachCount.query();

           

              var numAttachments = '0';

              // if no other attachments task does not have attachment

              if (attachCount.next()) {

                      numAttachments = attachCount.getAggregate("COUNT");

                      if (numAttachments > 0){

                              hasAttachment = 'true';

                      }

              }

              else {

                      hasAttachment = 'false';

              }

              //var timeNow4= new GlideDateTime();

                  //gs.log('has_attachment br: gliderecord query start date time is: ' + //timeNow4.getNumericValue(),'jwtest');    

      }

}

 

function hasAttachment(answer) {

      var inc = new GlideRecord('incident');

      inc.addQuery('sys_id',current.table_sys_id);

      inc.query();

 

      if(inc.next()) {

              inc.u_has_attachment = answer;

              inc.autoSysFields(false); //Don't set the lastUpdatedTime or the Simultaneous Update Alert will likely get triggered

              inc.setWorkflow(false); //Don't allow other business rules to run, otherwise multiple notifications will likely be sent

              inc.update();

      }

}

1 ACCEPTED SOLUTION

LRobinson
Giga Contributor

Hi,


I have created this solution to update the new "u_has_attachments" field on existing active records with attachments:



var rec = new GlideRecord('incident');


  rec.addActiveQuery();


  rec.query();



  while(rec.next()){


      if(rec.hasAttachments()){


          rec.u_has_attachments = 'true';  


          } else {  


          rec.u_has_attachments = 'false';  


          }  


    rec.setWorkflow(false);  


    rec.autoSysFields(false);


    gs.log("Incident: " + rec.getDisplayValue() + " has attachments");  


    rec.update();  


}  



I ran this from scripts-background and it updates the "u_has_attachment" field on the Incident record so that "true" or "false" displays as required in listview (or reports), and can then be used for sorting or filtering.


I modified the script you provided using the wiki "hasAttachments" script (GlideRecord - ServiceNow Wiki) to find this solution.


View solution in original post

19 REPLIES 19

LRobinson
Giga Contributor

Hi,


I have created this solution to update the new "u_has_attachments" field on existing active records with attachments:



var rec = new GlideRecord('incident');


  rec.addActiveQuery();


  rec.query();



  while(rec.next()){


      if(rec.hasAttachments()){


          rec.u_has_attachments = 'true';  


          } else {  


          rec.u_has_attachments = 'false';  


          }  


    rec.setWorkflow(false);  


    rec.autoSysFields(false);


    gs.log("Incident: " + rec.getDisplayValue() + " has attachments");  


    rec.update();  


}  



I ran this from scripts-background and it updates the "u_has_attachment" field on the Incident record so that "true" or "false" displays as required in listview (or reports), and can then be used for sorting or filtering.


I modified the script you provided using the wiki "hasAttachments" script (GlideRecord - ServiceNow Wiki) to find this solution.


Hi Drew!


I know this an old post, but hoping you can provide guidance.


Your BR code works great if it's an existing incident.


What i'm finding, is if we create a new incident and attach something, then click "update" or "Save" on the Incident, the flag doesn't change.


It that has attachments flag only changes if we add/remove attachments on existing incidents.



I can't seem to figure out why. Any thoughts?


So we have two BR's, one that runs on sys_attachement on insert/update/delete and one that runs on Task that only runs on insert.   Is that how yours are setup?


Hi Drew.


Thanks so much for your reply. I tried, that but just couldn't get the task/insert BR to work. So, I ended up going another route entirely.



  1) created new true/false field on the Incident.



  2) Configured that field, and on calculated value tab, have:


                        (function calculatedFieldValue(current) {


                              return current.hasAttachments();


                          })(current);



  3) then i just ran this background script to update all existing records.


var rec = new GlideRecord('incident');


  rec.addActiveQuery();


  rec.query();



  while(rec.next()){


      if(rec.hasAttachments()){


          rec.u_has_attachments = 'true';


          } else {


          rec.u_has_attachments = 'false';


          }


    rec.setWorkflow(false);


    rec.autoSysFields(false);


    gs.log("Incident: " + rec.getDisplayValue() + " has attachments");


    rec.update();


}



Working like a charm.


Thanks Again!


This only caution I have for you is that you are causing the system to do a fair bit of work when there are no changes every time the record is updated.