if incident record if their any attachment no need to show any alert .If attachment is not their show the alert

venkyp
Kilo Explorer

hi ,

9 REPLIES 9

shloke04
Kilo Patron

Hi,



If my understanding is correct, you need to check whether the Incident Record has any attachments added or not. In order to do so we need to write an before Insert/Update business Rule on the Incident table as per the script mentioned below:



Script:



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



  var att = new GlideAggregate('sys_attachment');


  att.addAggregate('COUNT');


  att.addQuery('table_name', current.getTableName());


  att.addQuery('table_sys_id', current.sys_id);


  att.query();


  var count = 0;



  if (att.next()) {


  count = att.getAggregate('COUNT');


  if (count<1) {


  gs.addInfoMessage("Please attach Attachment");


  current.setAbortAction(true);


  }


  }



})(current, previous);



find_real_file.png



find_real_file.png



Hope this helps.Mark the answer as correct/helpful based on impact.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Great it worked for me...but after I am saving the incident I am moving out of the incident page..how do I stay on the same form even after saving it?

 

Shishir Srivast
Mega Sage

You can create a OnSubmit on incident table to Glide the record on sys_attachment table. if there is any record then don't show alert else show alert to attach a file.



  1. function onSubmit() {  
  2. var sys_id = current.getUniqueValue();  
  3. var gr = new GlideRecord("sys_attachment");  
  4. gr.addQuery("table_name", gs.getTableName());  
  5. gr.addQuery("table_sys_id", sys_id);  
  6. gr.query();  
  7. if (!gr.next()) {  
  8. alert("You must attach a file to submit.");  
  9. return false;  
  10. }  
  11. }  

As recommended by Service Now Best practices, it's better not to Use Glide Record on a Client Side Script. for checking the count of attachments present or not on the form.



Glide Aggregate on the Server side would be the ideal choice to check for the Attachments added or not to the form for better System performance and to simply abort the Form submission if the Attachment has not been uploaded.



Regards,


Shloke


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke