Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Verify attachments exist?

corey125
Kilo Contributor

Hello all,

I am looking for a way to verify that 3 files are attached to the change request form, I dont need to care about what type of files or their names, I just need a way to make it mandatory to upload 3 files.

Thanks!

1 ACCEPTED SOLUTION

shloke04
Kilo Patron

Hi Corey,



You can write an Before Insert/Update Business Rule on Change Request Table with the script mentioned below:



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




  // Add your code here


  var rowcount=0;


var gr = new GlideRecord("sys_attachment");


gr.addQuery("table_name", "change_request");


gr.addQuery("table_sys_id", current.sys_id);


gr.query();


while(gr.next())


{


rowcount++;


}



if(rowcount<3){


gs.addInfoMessage("Attachment(s) is Missing!!");


current.setAbortAction(true);


}




})(current, previous);



find_real_file.png



Mention your conditions in the highlighted filter above   when you want to check for the Attachments to be mandatory.



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

View solution in original post

3 REPLIES 3

Alikutty A
Tera Sage

Hi,



What is the trigger for your validation? You need to query on the sys_attachment table and check the row count of attachment entries for the record and abort the action if it does not satisfy your count.



var gr=new GlideRecord('sys_attachment');


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


gr.query();


gr.getRowCount(); //number of attachments added




Thanks


PS: Hit like, Helpful or Correct depending on the impact of the response


shloke04
Kilo Patron

Hi Corey,



You can write an Before Insert/Update Business Rule on Change Request Table with the script mentioned below:



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




  // Add your code here


  var rowcount=0;


var gr = new GlideRecord("sys_attachment");


gr.addQuery("table_name", "change_request");


gr.addQuery("table_sys_id", current.sys_id);


gr.query();


while(gr.next())


{


rowcount++;


}



if(rowcount<3){


gs.addInfoMessage("Attachment(s) is Missing!!");


current.setAbortAction(true);


}




})(current, previous);



find_real_file.png



Mention your conditions in the highlighted filter above   when you want to check for the Attachments to be mandatory.



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

jonathan_77
Mega Expert

Hey,

I am looking at implementing something just like this.  I need to make sure a document is attached to a record before it is submitted, I have a question about the code mentioned above.  In this example, the user wants to make sure 3 documents are attached to the Change Request record (i.e., change_request table).  What would the "table_name" be in this code?  We are querying the sys_attachment table, we are referencing the change_request table??  And I did not know that a table itself had a sys_id.  Am I understand this correctly? 

Lets just say that I am creating this BR on the hr_task table and I want to make sure that 3 attachments are attached to every HRTask record before it can be submitted or updated.  What would my code look like?

These are the two lines of code that I am confused about:

gr.addQuery("table_name", "change_request");

gr.addQuery("table_sys_id", current.sys_id);

 

Thanks!!

Jonathan