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

I thought of that, which is why I dont even display the field on the incident itself (no need). It's only used for reports or lists. That should reduce the workload when viewing the incident, right?


This from the Wiki indicates it will run when BR's run


http://wiki.servicenow.com/index.php?title=System_Dictionary#gsc.tab=0


Determines whether the value of the field is calculated from other values. If selected, use the Calculation field to define how the calculation is performed. When sorting or grouping by a calculated field, the sort order is based on the field value from the last time the field was updated, not the last time the field was displayed. Note: In relation to business rules, calculated fields are populated first before any business rule, even a before business rule, is run. Calculated fields are then populated again if necessary after any before business rules run.


Has anyone run into a situation where rec.hasAttachments () = false even though there are attachments attached to the record?



Running into a situation where entries with attachments are not showing up as rec.hasAttachments () = true.


I seem to recal that happening when there were spaces in the name of the file. Have you tried to see if you name your file xxx_xxx_xxx if that works?


No I figured it out.   In an attempt to try to attach the field to all task related entries, I tried running hasAttachments() against the Task table.



Whereas it should be done against the extended tables.   Oops.