- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-23-2014 11:54 AM
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();
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2014 06:50 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2014 11:58 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2014 12:20 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2014 12:34 PM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2014 12:55 PM
So close, it updated all the records to "true", even those without an attachment.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2014 12:59 PM
Its probably this then
- gr.u_has_attachments = 'true';
- } else {
- 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.