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

Jaspal Singh
Mega Patron
Mega Patron

Hi Simran,

 

Using GlideRecord in client script is not an ideal option. You can try a before update business rule that runs on your required task table with below script & condition as 

State | changes to  | closed

(function executeRule(current, previous /*null when async*/) {
	var attach = new GlideRecord('sys_attachment');
	attach.addQuery('table_sys_id', current.table_sys_id);
	attach.query();
	if(!attach.next()){
   gs.addErrorMessage('Task cannot be closed without Attachment');
		current.setAbortAction(true);
	}
})(current, previous);

This sets the state to closed complete and only changes the state to open when you refresh. It also wipes out the assigned to field.

Refresh for previous state can be avoided by using 

action.setRedirectURL(current);

just after 

current.setAbortAction(true);

I am guessing Assigned To is being filled when it is being closed as well & thus not stored in database still & thus wipes out.

 

In case you can also use Display Business Rule & Client script as below that runs onSubmit()

1. Display Business Rule.

(function executeRule(current, previous /*null when async*/) {
	 
g_scratchpad.hasAttachments = current.hasAttachments();
	
})(current, previous);

 

2. onSubmit() client script as below.

function onSubmit(){
  
if (g_scratchpad.hasAttachments)
{ 
//do nothing
}
else
{
alert('Attachment required');
}

}

Is there a way to populate the "assigned to" field with the user after the form is reloaded?