Search question - Can you search to see if something has an attachment?

Community Alums
Not applicable

I was asked the question today, and I am a new user so I was not sure.

 

Can you search items in ServiceNow by if they have an attachment or not?

 

Something like:

 

attachment == true - return the record in the search

attachment == false - omit the record.

 

I hope that question makes sense.

 

Thanks!

4 REPLIES 4

Mark Stanger
Giga Sage

Not natively, but it is possible to create a new field value and populate it if the record has attachments.   Then you could filter on that field value.



Display Whether Tasks Have Attachments in List View - ServiceNow Wiki


This is what we do. We have a field on the task table an use a BR to populate it with the number of attachments. This way it's always there, every record any time you need it!


amadosierra
Kilo Guru

Hi Kris,



If what you're looking for is a simpler way to test if a record has attachments or not, you can do so with the hasAttachments() function.


For example, a condition can be built as follows:



current.hasAttachments() && current.state.changes()



Or loop through some records:



//Check for attachments and add link if there are any


  var attachment_link = '';


  var rec = new GlideRecord('sc_req_item');


  rec.addQuery('sys_id', current.request_item);


  rec.query();


  if(rec.next()){


    if(rec.hasAttachments()){


  attachment_link = gs.getProperty('glide.servlet.uri') + rec.getLink();


    }    


}



Reference: GlideRecord - ServiceNow Wiki


Kalaiarasan Pus
Giga Sage

we do have something like this and we make use of dynamic filter for this ... I have setup a demo .. Refer this and see if that helps ...



Details:


First, we need a script include to check the records that has attachment and return the id's of the records that has attachment. Next, we need to call the script in the filter of the table needed...



Sample script:


function checkIfRequestHasAttachment()


{


      var incidentWithAttachments = [];


      var rec = new GlideRecord('incident');


      rec.addActiveQuery();


      rec.query();


      while(rec.next()){


              if(rec.hasAttachments()){


                      incidentWithAttachments.push(rec.sys_id.toString());


              }


      }


      return incidentWithAttachments.toString();


}



Call the script in the filter as below



1.png



Demo you can check on



url : demo001.service-now.com/login.do


username and password : admin