The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Check Attachment Script

khsieh
Kilo Explorer

I am trying to implement the check attachment script for our incident application, but have not been able to get the script to run. Has anyone tried to implement this? I could use some pointers, I am not familiar with scripting only the general concepts.

I have followed the steps of creating the customized field as well, but have been extremely unsuccessful.

Thanks!

Script:
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();
}
}

5 REPLIES 5

younes_sebti
Kilo Sage

Not sure about this. I think Oliver Dösereck was right. You must make sure that your task table has the u_has_attachments field created.


Check your dictionary


(


https://yourinstance.service-now.com/sys_dictionary_list.do?sysparm_query=name%3Dtask%5Eelement%3Du_...


)








I would take another approach.



Could you try this?



Business Rule on table sys_attachment


After


insert, update and delete checked



Script :


var task = new GlideRecord('task');


if (task.get(current.table_sys_id)){


  var attachment_found = false;


  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.hasNext()) {


      attachment_found   = true;


  }


  if (task.u_has_attachments != attachment_found) {


          task.u_has_attachments = attachment_found;


          task.u_has_attachments.update();


}