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

If it works to mark them true then I would just mark them all false and run the script to fix the ones that should be true.


LRobinson
Giga Contributor

It only marks them true when I add a new attachment and update the record. I have tried running the script from background scripts by it doesn't work.   I am not sure what I am doing wrong.


Oh...   Not sure why your code is so complicated but just do the below, assuming your "has attachment" field is in the task table and you only want to update active tasks.



var gr = new GlideRecord("task");
gr.addActiveQuery();
gr.query();
while (gr.next()) {
verifyAttachments(gr);
}



function verifyAttachments(gr){
var at = new GlideRecord('sys_attachment');
at.addQuery('table_sys_id', gr.table_sys_id);
at.addQuery('table_name', gr.table_name);
at.query();
if(at.next()){
  gr.u_has_attachments = 'true';
} else {
  gr.u_has_attachments = 'false';
}
gr.setWorkflow(false);
gr.autoSysFields(false);
gr.update();
}


LRobinson
Giga Contributor

So close, it updated all the records to "true", even those without an attachment.


Its probably this then



  1.   gr.u_has_attachments = 'true';  
  2. } else {  
  3.   gr.u_has_attachments = 'false';  


take out the single quotes maybe.   either way with a few gs.log statements you should be able to find the issue.