onsubmit client script to check for attachments before submitting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2023 08:16 AM
Hello Experts,
I need a onsubmit client sprint which should check for attachments if the request_type = grant or modfiy.
system should not allow the request to be submitted until the attachment is attached.
for revoke it should not check for attachment and allow order now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2023 08:20 AM - edited 01-11-2023 08:29 AM
Hi,
Please refer below script
try {
length = angular.element("#sc_cat_item").scope().attachments.length;
} catch(e) {
length = -1;
}
if(length>0)
alert(""attachment is there);
return length;
You can create a function in UI script as below
In the portal theme widget's JS include related list create a record as below
Now in onsubmit client script use below script
var count = getSCAttachmentCount();
alert("Attcah Count: "+count )
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2023 08:33 AM
Hello @Anubhav Srivas1 ,
You can try below onsubmit client script
function onSubmit() {
var cat_id = gel('sysparm_item_guid').value;
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_name", "sc_cart_item");
gr.addQuery("table_sys_id", cat_id);
gr.query();
if (!gr.next()) {
alert("You must attach a file to submit.");
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2023 09:11 PM
Hello Omkar,
i'm using this script but getting an error
function onSubmit() {
if (g_form.getValue('request_type') == 'grant'); { // This is the field value of the question you want to check
try {
var msg = 'This request requires an attachment. Please review the form and try again.';
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
alert(msg);
return false;
}
}
//For Service Portal
catch (e) {
var count = getSCAttachmentCount();
if (count <= 0) {
alert(msg);
return false;
}
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-11-2023 02:53 PM - edited 01-11-2023 02:55 PM
Please try the below catalog client script (onSubmit)
function onSubmit() {
if (g_form.getValue('your_variable') == "your_variable_value") { // This is the field value of the question you want to check
try {
var msg = 'This request requires an attachment. Please review the form and try again.';
var attachments = document.getElementById('header_attachment_list_label');
if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none') {
alert(msg);
return false;
}
}
//For Service Portal
catch (e) {
var count = getSCAttachmentCount();
if (count <= 0) {
alert(msg);
return false;
}
}
}
}