- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi Team ,
We have a catalog item called “AWS Support”. Inside it, there is a variable called “Type of Request”.
One of the options for this variable is “Create AWS AD Group”.
If the user selects this option and tries to submit the form without adding an attachment, the form should not be submitted. Instead, the system should show an error message asking the user to attach the required file.
function onSubmit() {
var requestType = g_form.getValue('type_of_request');
if (requestType == 'Create AWS AD Group') {
var attachmentCount = g_form.getValue('attachments'); // this only works with attachment mandatory check in later versions
// For safer check - use g_form.hasAttachments() if your instance supports
if (!g_form.hasAttachments()) {
alert("Attachment is mandatory when selecting 'Create AWS AD Group'. Please attach a file before submitting.");
return false; // Prevent submission
}
}
return true;
}
the script which i have written it is not working - what was the error .
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @nameisnani ,
"If the user selects this option and tries to submit the form without adding an attachment, the form should not be submitted. Instead, the system should show an error message asking the user to attach the required file."
It looks like - YOu want this field to be mandatory and when user is leaving blank, it should display error.
Regards,
Nikhil Bajaj
Regards,
Nikhil Bajaj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @nameisnani
Kindly make changes as per the below code. It should work.
Client Script
function onSubmit() {
var requestType = g_form.getValue('type_of_request');
if (requestType == 'Create AWS AD Group') {
var sysid = g_form.getValue('sysparm_item_guid');
var ga = new GlideAjax('CheckAttachment');
ga.addParam('sysparm_name', 'hasAttachment');
ga.addParam('sysparm_sysid', sysid);
ga.getXMLWait();
var answer = ga.getAnswer();
if (answer == 'false') {
alert("Please attach something before submitting this request.");
return false;
}
}
return true; // Allow submission if attachment exist
}
Script Include
var CheckAttachment = Class.create();
CheckAttachment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasAttachment:function() {
var sysId = this.getParameter('sysparm_sysid');
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'sc_cart_item');
gr.addQuery('table_sys_id', sysId);
gr.query();
return gr.hasNext().toString(); // Returns 'true' or 'false'
},
type: 'CheckAttachment'
});
Regards,
Sourabh