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.

Onsubmit Client Script to check Attachments on Record producer

Chaisai
Kilo Expert

Hello,

 

I have a record producer on a custom table in custom application which has check box if checked need to attach the files , so i have been using below code it is working but even after attaching on the form i am getting same Alert and unable to submit the form , any help in this is much appreciated!

 

Onsubmit Client script code :

function onSubmit() {

attachOne = g_form.getValue('one');
attachTwo = g_form.getValue('two');
attachThree = g_form.getValue('three');

if(attachOne == 'true' || attachTwo == 'true' || attachThree == 'true') //checking if any of the attachment checkbox is ticked 
{

var att = new GlideRecord("sys_attachment");
att.addQuery("table_name", "sc_cat_item_producer"); 
att.addQuery("sys_created_by", g_user.userName); 
att.query();
if(!att.next())
{
alert("Please attach the Required Documnet to proceed with submitting.");
return false;
}

}

}

10 REPLIES 10

vinothkumar
Tera Guru

PLease refer the below thread

 

https://community.servicenow.com/community?id=community_question&sys_id=ec764fe1db1cdbc01dcaf3231f9619e2

 

Change your code  as below

 

 

  1. var sys_id = gel('sysparm_item_guid').value;          
  2. var attachment = new GlideRecord('sys_attachment');          
  3. attachment.addQuery('table_name','table name');   //my custom table is "table name"  
  4. attachment.addQuery('table_sys_id',sys_id);          
  5. attachment.query();      
  6. if (!attachment.next()) {          
  7.       alert ("PLEASE ATTACH Attachment");      
  8.   return false;  
  9. }

Quick question :

 

About this line 

attachment.addQuery('table_name','table name');   //my custom table is "table name" 

 

So the table name gonna be where my Record producer sits which is "sc_cat_item_producer"

 

or the Custom table on which the record producer creates the record ?

In table name, you have to pass the table name where your record is going to create and not sc_cat_item_producer

Thanks for your response!

 

I have gotta workout this on Service Portal so it is not working with above script. any thoughts ?