How to allow only 5 attachments and restrict to pdf format in service catalog?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2023 08:25 AM
I have a catalog items. --> Need to restrict the user to attach only 5 attachments and all the attachments has to be in pdf format only.
Please suggest how to achieve this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2023 08:39 AM - edited 02-28-2023 08:49 AM
Script: onSubmit
Order: 100
Note: GlideRecord is not recommended in Client scripts. but you don't have other way to check PDF format
function onSubmit() { //Type appropriate comment here, and begin script below 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; } else { var r = gr.getValue('file_name'); //file_name is a field on sys_attachment table which stores MIME types of attached file. //alert('date'+r); if(r.indexOf('txt') != -1 ) { // alert ('Thanks for udate'); } else if(r.indexOf('pdf') != -1 ) { // alert ('Thanks for udate'); } else { alert ('Invalid Insert. Please remove the attachment and attach .txt and .pdf format files only'); gr.setAbortAction(false); gr.getValue('file_name').clearOptions('file_name'); return false; } //Type appropriate comment here, and begin script below } }
Script: onSubmit
Order: 200
Note: Ensure Isolate Script field is set to False for this Catalog Client Script to ensure DOM manipulation works
function onSubmit() {
//Type appropriate comment here, and begin script below
var countRequired = 5;
if(window == null){
// portal
if(this.document.getElementsByClassName('get-attachment').length > countRequired) {
alert('You can't add more than 5 attachments before submitting this request.');
return false;
}
}
else{
// native view
var length = $j("li.attachment_list_items").find("span").length;
if(length > countRequired){
alert('You can't add more than 5 attachments before submitting this request.');
return false;
}
}
}
Bharath Chintala
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2023 10:25 AM
Hi @BharathChintala ,
I am getting error - "gel is not defined".
MY catalog client script is in Scoped Application.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2023 11:44 PM
@Bharath did you check isolate script check box to false?
Bharath Chintala
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2023 10:40 AM
Hi @BharathChintala ,
var cat_id = g_form.getValue('sysparm_item_guid');
This script is working in Native view but its not working on portal.
Can you pls suggest is there anything that needs to be done in portal