I want to get the attachment length for the catalog item from service portal before request submiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi All,
I want to get the attachment length for the catalog item from service portal before request submit
I have one variable called "request approval" which is check box
if above check box is true,
I need to make the attachment mandatory before submitting the request
I am trying to get the attachment length using onSubmit client but not working as expected please find the below scripts, I have tested in different ways,
Please try the above requirement in your PDIs and provide the working code here
ClientScript
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12m ago
Hi @Shantharao
could you try this script
var allowSubmit = false;
function onSubmit() {
// If already validated, allow submit
if (allowSubmit) {
return true;
}
// Check if checkbox variable is true
if (!g_form.getValue('request_approval')) {
return true; // no attachment required
}
var sysId = g_form.getUniqueValue();
var ga = new GlideAjax('AttachmentChecker');
ga.addParam('sysparm_name', 'getAttachmentCount');
ga.addParam('sysparm_sys_id', sysId);
ga.getXMLAnswer(function(answer) {
var count = parseInt(answer);
if (count === 0) {
g_form.addErrorMessage('Please add at least one attachment before submitting.');
allowSubmit = false;
} else {
allowSubmit = true;
g_form.submit(); // submit AFTER validation
}
});
return false; // ALWAYS stop initial submit
}
Script Include (Client Callable)
var AttachmentChecker = Class.create();
AttachmentChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAttachmentCount: function () {
var sysId = this.getParameter('sysparm_sys_id');
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', sysId);
gr.query();
return gr.getRowCount().toString();
},
type: 'AttachmentChecker'
});
Regards,
Poonkodi
