attachment fails

tGhadage
Tera Contributor

Hi Guys,

 

(function executeRule(current, previous /*null when async*/) {

    // Add your code here
   var attachment_file = new GlideRecord('sys_attachment');
   //attachment_file.addAggregate('COUNT');
   //attachment_file.addQuery('table_name',current.getTableName());
   attachment_file.addQuery('table_sys_id',current.sys_id);
   attachment_file.query();
   //
   
   while(attachment_file.hasNext()){
   gs.addErrorMessage('Please attach the requried documents before closing the task');
   current.state=previous.state;
   current.setAbortAction(true);
}
})(current, previous); 
 
using this code i am trying to make attachment mandatory but even when i attach the attachment still its not getting close complete.
 
thanks!! 
please help this out.
3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@tGhadage  Please update your code as follows.

 

(function executeRule(current, previous /*null when async*/) {

    // Add your code here
   var attachment_file = new GlideRecord('sys_attachment');
   //attachment_file.addAggregate('COUNT');
   //attachment_file.addQuery('table_name',current.getTableName());
   attachment_file.addQuery('table_sys_id',current.sys_id);
   attachment_file.query();
   //
   
   if(!attachment_file.hasNext()){
   gs.addErrorMessage('Please attach the requried documents before closing the task');
   current.state=previous.state;
   current.setAbortAction(true);
}
})(current, previous); 
 

Please mark the response helpful and accepted solution if it manages to address your question.

Voona Rohila
Kilo Patron
Kilo Patron

Hi @tGhadage  

Please change while to if and add ! check in your code.

 if (!attachment_file.hasNext()) {
        gs.addErrorMessage('Please attach the requried documents before closing the task');
        current.state = previous.state;
        current.setAbortAction(true);
}

hasNext() is returning true and while is going through loop continuously


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Bhavya11
Kilo Patron

Hi @tGhadage  ,

 

Please try below script Business Rule:

 

Bhavya11_0-1721990265071.png

 

then script 

 

(function executeRule(current, previous /*null when async*/) {

 var attachment_file = new GlideRecord('sys_attachment');

    // Query for attachments related to the current record
    attachment_file.addQuery('table_name', current.getTableName()); // Ensure it's checking the correct table
    attachment_file.addQuery('table_sys_id', current.sys_id); // Query based on the current record's sys_id
    attachment_file.query();

    if (!attachment_file.hasNext()) {
        gs.addErrorMessage('Please attach the requried documents before closing the task');
        current.state = previous.state; // Revert the state to its previous value
        current.setAbortAction(true); // Prevent closing the task
	}



})(current, previous);

 

after hit the CLOSE TASK button or change state to Close complete then it will throw error 

Bhavya11_1-1721990733141.png

after that refresh the page and see the SCTASK still in previous state not to close 

 

If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."

 

 

Thanks,

BK