Making Attachment Mandatory OnSubmit Catalog Client Script

ankitaankit
Tera Contributor

Hi Team,

I have below OnSubmit Catalog Client Script which checks attachment asynchronously, I have to use asynchronous server call only, but it will not wait for the response from the server and will let the system submit the form. It should check and alert the user for those mandatory attachments before the form submission.
Could you please suggest me the best approach for it, below is my code.

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var catId = g_form.getParameter("sysparm_item_guid");
var ajaxAttachGlideAJAX = new GlideAjax('GetAttachment'); //calling S.I by GlideAjax
ajaxAttachGlideAJAX.addParam('sysparm_name', 'getAttachment');
ajaxAttachGlideAJAX.addParam('sysparm_catID', catId);
ajaxAttachGlideAJAX.getXML(callback);

function callback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'false') {
alert(" Please add a file ");
return false; //abort form to submit
} else {
return true;
}

}
}

Please help me with another possible way or what correction can be done to existing code to achieve the same.

2 REPLIES 2

Sanjay191
Tera Sage

Hello @ankitaankit 
if you want to get the form data in client side, you should use the g_form.getValue("backend name of the field ").
and getParameter  is used in script include 

and Please check the below article how we can use the Glide Ajax

https://docs.servicenow.com/bundle/xanadu-api-reference/page/app-store/dev_portal/API_reference/Glid...


Also you can use the make the attachment mandatory

Sanjay191_0-1730719560463.png

 


If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You

OlaN
Giga Sage
Giga Sage

Hi,

There should not be a need to script this.

There is a built in way to make attachments mandatory on Catalog items.

Just check the field "mandatory_attachment" on the Catalog item.