- 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-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
02-17-2017 09:30 AM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2017 12:33 PM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2017 06:12 AM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-21-2017 06:27 AM
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.