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

Yes, you can but which user should be auto-populated? Logged in user?

You can use the same approach of Display Business Rule & onLoad() client script for getting the user value populated in Assigned To field.

Also, if it is not in attachments context so I would suggest you to keep it as a separate thread so as to avoid mix of issues which may confuse future readers.

The form did not reload with 

action.setRedirectURL(current);

when placing this after:

current.setAbortAction(true);

asifnoor
Kilo Patron

Hi,

Try like this in your onSubmit script.

function onSubmit(){
  if (this.document.getElementsByClassName('get-attachment').length == 0) {
    g_form.addErrorMessage(getMessage('attachment_mandatory'));
    return false;
  }

}

Mark the comment as a correct answer and helpful if this helps.

Hello Simran,

Did you try the solution that i gave above? 

Simran9
Tera Contributor

I don't think that part of the script is the issue. My alert("simran") is not popping up for the task where it checks for that short description