Has Attachment business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2011 10:50 AM
I've implemented the "Has Attachment" business rule on the task table, which works great. Does anyone know if there is a way to run the rule (or modify it) to run against existing records that may have an attachment and update the flag?
http://wiki.service-now.com/index.php?title=Useful_Attachment_Scripts
checkAttachment();
function checkAttachment(){
// if inserting then the task has an attachment
if (current.operation() == 'insert') {
hasAttachment('true');
}
// if deleting attachment check for other attachments
if (current.operation() == 'delete') {
var attachments = new GlideRecord('sys_attachment');
attachments.addQuery('table_sys_id',current.table_sys_id);
attachments.query();
// if no other attachments task does not have attachment
if (!attachments.next()) {
hasAttachment('false');
}
}
}
function hasAttachment(answer) {
var task = new GlideRecord('task');
task.addQuery('sys_id',current.table_sys_id);
task.query();
if(task.next()) {
task.u_has_attachments = answer;
task.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2011 10:58 AM
Since I'm assuming there's less attachment records than task records I would run a background script that queries sys_attachment for table_name of any of the task types. Then for each result, get the task record from sys_attachment.table_sys_id and set the flag to true.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2011 11:01 AM
Forgot to add, you should also disable workflow and date updates.
If "gr" is the task GlideRecord just add the following before update().
gr.autoSysFields(false); //prevents updated on from changing
gr.setWorkflow(false); //disables business rules if you don't need since it's more efficient
gr.update();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2011 06:51 AM
John
Thanks for the info. Would you happen to have a sample script that I could run or modify? I don't have a strong scripting background, so any help you can offer is great.
Rick
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-05-2011 03:52 PM
First off, a disclaimer: I wouldn't recommend anyone running background script or actually building any scripts unless you understand the full impact and risk of the scripts.
With that out of the way....
If you're able to understand the following script and risks this should do what you need. The script is designed to be used in the background scripts module. The update method is currently commented since I also recommend logging the results of a change before you actually make the update.
//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
}
}
}
Hope that helps.
By the way, Knowledge11 Conference has a number of scripting related training sessions for admins. Otherwise, you might want to contact your account rep to get some assistance from Professional Services.