- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Dear Community,
I need some suggestion to make " Add attachments" mandatory in catalog item and prevent from submission the catalog item form based on Yes/NO Type Variable selection. If Variable called "do_have_approval" is Yes then make the " Add attachments" mandatory in catalog item and prevent from submission the catalog item form, if attachment is attached then allow to submit the form.
I am trying to achieve this through catalog client script but its not working.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Naresh_5120 ,
I tried your problem in my PDI and it works for me please check solution below
I created 2 variables on my Catalog item 1. Is Approval(Yes/No), 2. Attachment (attachment type)
I created one OnSubmit Client script and add below code
function onSubmit() {
//Type appropriate comment here, and begin script below
var approval = g_form.getValue('is_approval'); // Yes/No variable
if (approval === 'Yes') {
var attCount = g_form.getControl('add_here').attachments
? g_form.getControl('add_here').attachments.length
: 0;
if (attCount === 0) {
g_form.addErrorMessage("Attachment is required when approval is Yes.");
return false;
}
}
return true;
}
Result
When I select yes and try to submit it shows me error
Please mark my answer correct and helpful if this works for you
Thanks and Regard,
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Naresh_5120 ,
I tried your problem in my PDI and it works for me please check solution below
I created 2 variables on my Catalog item 1. Is Approval(Yes/No), 2. Attachment (attachment type)
I created one OnSubmit Client script and add below code
function onSubmit() {
//Type appropriate comment here, and begin script below
var approval = g_form.getValue('is_approval'); // Yes/No variable
if (approval === 'Yes') {
var attCount = g_form.getControl('add_here').attachments
? g_form.getControl('add_here').attachments.length
: 0;
if (attCount === 0) {
g_form.addErrorMessage("Attachment is required when approval is Yes.");
return false;
}
}
return true;
}
Result
When I select yes and try to submit it shows me error
Please mark my answer correct and helpful if this works for you
Thanks and Regard,
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
You are using a strict mode catalog client script, so attachment APIs like getAttachments or getEncodedRecordAttachments are blocked. Because of this, we cannot validate attachments on the client side.
The correct solution is to use a simple onSubmit client script for the Yes or No check, and then enforce attachment validation on the server side.
Step 1. Client Script onSubmit
function onSubmit() {
var needApproval = g_form.getValue('do_have_approval');
if (needApproval == 'Yes') {
g_form.addErrorMessage('Attachment is required Please upload before submitting.');
return false;
}
return true;
}Step 2. Server Side Validation (Business Rule)
This ensures the form cannot be submitted without an attachment if the user selects Yes.
Table sc_req_item
When Before insert
(function executeRule(current,previous) {
var needApproval = current.variables.do_have_approval + '';
if (needApproval == 'Yes') {
var att = new GlideAggregate('sys_attachment');
att.addQuery('table_sys_id', current.sys_id);
att.addAggregate('COUNT');
att.query();
var count = 0;
if (att.next()) {
count = parseInt(att.getAggregate('COUNT'), 10);
}
if (count === 0) {
gs.addErrorMessage('Attachment is required when approval is Yes.');
current.setAbortAction(true);
}
}
})(current,previous);This combination works in strict mode and is fully supported. Even if the user skips the client message, the server will block the submission until an attachment is added.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @Naresh_5120 ,
This can be done without any script as well if you want to. What you need to do is:
1) Create a UI Policy for the Yes/No field and in the 'When' condition mention the value to be 'Yes'
2) In the UI policy action, select the attachment variable and make 'mandatory' option as true.
*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.