The CreatorCon Call for Content is officially open! Get started here.

How can I make attachments mandatory on a task?

Simran9
Tera Contributor

I'm trying to make attachments mandatory on a certain task. I used a catalog client script for this but it's not working. Is there something wrong with my script?

find_real_file.png

function onSubmit() {
var state = g_form.getValue('state');
var i = 0;
var short_desc = g_form.getValue('short_description');
        if (short_desc == "SCC4 access – Close System Attach Logs"){
        alert("simran");
             if (state == '3'){
             var sysid = g_form.getUniqueValue();
             var attach = new GlideRecord('sys_attachment');
             attach.addQuery('table_sys_id',sysid);
             attach.query();
                     while(attach.next()){
                     i++;
                     }
                           if (i >=1 ){
                           return true;
                           }
                               else {
                               alert("You must attach the required forms prior to closing this task");
                               return false;
}
}
}
}

14 REPLIES 14

I'm assuming you blanked out your super-secret Catalog Item name only for the screen shot and there really is one populated.  If not, you'll want to do that.  If you're still not getting your "simran" alert then the task short description is not exactly = "SCC4 access – Close System Attach Logs".  I would copy and paste from the task to the script to make sure of spacing, case, etc.  The scripts I provided are ones I have in use to enforce mandatory attachments on certain tasks.

I did copy and paste the short description. I suspected this before but it's not the issue.find_real_file.png

There must be an explanation.  Start your catalog client script like this to verify that it is running, and what it is detecting as the short description.

function onSubmit() {
var short_desc = g_form.getValue('short_description');
alert('Short Description=' + short_desc + '=';
}

Confirm that the alert is

=SCC4 access – Close System Attach Logs=

no extra spaces before or after like

= SCC4 access – Close System Attach Logs=

or

=SCC4 access – Close System Attach Logs =

You can also try just using the beginning, or ending of the short description text in your if condition - in case the dash is throwing it off

if(short_desc.indexOf('SCC4 access') > -1){
alert("simran");
...

or

if(short_desc.indexOf('SCC4 access') > -1 &&
short_desc.indexOf('
Close System Attach Logs
') > -1
){
alert("simran");
...

Shantharao
Kilo Sage

Hi,

 

follow the below link you can find different ways to make attachment mandatory,

please tick applies on catalog task in the client script

 

https://community.servicenow.com/community?id=community_question&sys_id=dd618729db98dbc01dcaf3231f96...

 

please hit help and mark answer as correct based on the impact

Brad Bowman
Kilo Patron
Kilo Patron

Assuming you are not running this in the Service Portal, change your original onSubmit catalog client script to this.

function onSubmit() {
 var state = g_form.getValue('state');
 var short_desc = g_form.getValue('short_description');
 if (short_desc == "SCC4 access – Close System Attach Logs"){
  if (state == '3'){
   var sysid = g_form.getUniqueValue();
   var ga = new GlideAjax('CheckAttachment');
   ga.addParam('sysparm_name', 'attached');
   ga.addParam('sysparm_sysid', sysid);
   ga.getXMLWait();
   var answer = ga.getAnswer();
   if(answer == 'false') {
    alert("You must attach the required forms prior to closing this task");
    return false;
   }
  }
 }
}

Then create a Script Include with the same name as is used in the GlideAjax call, ensuring that the Client callable box is checked.  Your script will look like this

var CheckAttachment = Class.create();
CheckAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, { 

 attached: function() {
  var answer = 'false';  
  var id = this.getParameter('sysparm_sysid');
  var attachment = new GlideRecord('sys_attachment');
  attachment.addQuery('table_name', 'sc_task');
  attachment.addQuery('table_sys_id', id);
  attachment.query();
  if(attachment.next()){
   answer = 'true';
  }
  
  return answer;
 },
 type: 'CheckAttachment'
});