If task has attachments

Nishant16
Tera Expert

I have a True/false field "u_attachment" on sn_customerservice_task table to show if the task contains attachments. Below Business rule was created to set the checkbox, on insert it is working correctly when attaching attachments to the case task, however when i am removing the attachments it is not setting the checkbox to false, can someone help what i am missing?

BR written on sys_attachment table:

Condition: current.table_name == "sn_customerservice_task"

(function executeRule(current, previous /*null when async*/) {
    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('sn_customerservice_task');
	task.addQuery('sys_id',current.table_sys_id);
	task.query();
	if(task.next()) {
	task.u_attachment = answer;
	task.setWorkflow(false);
    task.autoSysFields(false);
	task.update();
	}
}
    
})(current, previous);
3 REPLIES 3

panda1
Kilo Guru

current.operation() == 'delete'

This is delete record

Delete attachment 

It should be current.operation() == 'update'

 

Hi Panda,

This doesn't work either!

Sorry, I didn't see it clearly.

BR written on sys_attachment table,I don't see here

1.Do you select Insert and delete in BR condition

 

2.use after Business rule

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');
}

When the before business rule is used, the DB data has not been deleted when the query is executed, so next = true

 

find_real_file.png