- 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 01:57 AM
You are saying yout BR is running on Attachments table. In this case, current refers to the attachment itself, not the corresponding incident record. So I do not quite understand what this condition is for because normally attachments do not have attachments:
... || current.hasAttachments()
Similarly, when you are processing a delete, I think you should be using current.table_sys_id instead of current.sys_id in the following piece of code:
attachCount.addQuery('table_sys_id',current.sys_id);
Otherwise, you are referring to the sys_id of the attachment itself.
Finally, when you get the count of attachments, you assign true or false value to the hasAttachment variable instead of calling the hasAttachment() function defined below.
If you fix these inconsistencies, your script will look similar to this:
checkAttachment();
function checkAttachment() {
if (current.operation() == 'insert')
hasAttachment('true');
if (current.operation() == 'delete') {
var attachCount = new GlideAggregate('sys_attachment');
attachCount.addQuery('table_sys_id', current.table_sys_id);
attachCount.addAggregate('COUNT');
attachCount.query();
var numAttachments = 0;
if (attachCount.next()) {
numAttachments = attachCount.getAggregate('COUNT');
if (numAttachments > 0)
hasAttachment('true');
} else {
hasAttachment('false');
}
}
}
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);
inc.setWorkflow(false);
inc.update();
}
}
By the way, I noticed you were assigning string values of 'true' and 'false' to u_has_attachment field? Is it a string field?
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2014 07:58 AM
Hi, thank you for the help. The field is boolean - True/False. I tried your modifications, but the functionality remains the same.
The field is still "true" if I delete/remove the attachment, and the listview does not update to show "true" for all current records with attachments. The only thing working properly is when I add an attachment and update, then the field updates to "true".

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2014 10:39 AM
This is the code we did almost 4 years ago and as far as I am aware we have had no issues with it.
//This BR is used to mark a task as having attachments or not so
//that a paper clip can be displayed on the list view in the number field.
if(current.operation() == 'insert' || current.operation() == 'update'){
hasAttachments();
} else if(current.operation() == 'delete'){
verifyAttachments();
}
function hasAttachments(){
var at = new GlideRecord(current.table_name);
at.addQuery('sys_id', current.table_sys_id);
at.query();
if(at.next()){
if(at.u_has_attachments != 'true'){
at.u_has_attachments = 'true';
at.update();
}
}
}
function verifyAttachments(){
var at = new GlideRecord('sys_attachment');
at.addQuery('table_sys_id', current.table_sys_id);
at.addQuery('table_name', current.table_name);
at.query();
if(at.next()){
} else {
var tsk = new GlideRecord(current.table_name);
tsk.addQuery('sys_id', current.table_sys_id);
tsk.query();
while(tsk.next()){
tsk.u_has_attachments = 'false';
tsk.update();
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2014 11:56 AM
Thank you!
That one works great when I add or delete attachments.
I am still working on updating existing records using this background script that looks like it should work, however it is only identifying the records with attachments, not updating as required:
- //get all task tables for an instance
- var tu = new TableUtils("task");
- var tables = tu.getAllExtensions();
- //get task related attachments
- var gr = new GlideAggregate("sys_attachment");
- gr.addQuery("table_name", "IN", tables); //query for table_name IN tables array
- //group by table name and id
- gr.groupBy("table_name");
- gr.groupBy("table_sys_id"); //group by task sys_id so we don't process multiple attachments for a single task
- gr.query();
- while (gr.next()) {
- //process each unique attachment task
- var table = gr.table_name;
- var id = gr.table_sys_id;
- //get task record
- var task = new GlideRecord("task");
- if (task.get(id)) {
- //we have a valid task record
- if (!task.u_has_attachments) {
- //only update if it's not already set
- task.u_has_attachments = true;
- task.setWorkflow(false);
- task.autoSysFields(false);
- gs.log("TASK: " + task.getDisplayValue() + " has attachments");
- //just log for now to test
- task.update(); //uncomment to actually update the record
- }
- }
- }