Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to check attachment before on submit client script in catalog item

dheeru_1994
Tera Contributor

Hii All 
i am writing this script in catalog item to know that item has attachment attached or not  on submit client script but 
error getting 
below is the code 

 

 

 

function onSubmit() {
    // Check if attachments exist
    var attachment = g_form.getAttachmentList();
    
    if (attachment.length > 0) {
        // If attachments are found, allow submission
        alert('Attachments found.');
        return true;
    } else {
        // If no attachments found, prevent submission and prompt user
        alert('Please attach a file.');
        return false;
    }
}

 

 

 

 

when i order item   error occure  Error MessageonSubmit   script error: TypeError: g_form.hasAttachmentList is not a function:
function () { [native code] }
glide record and script include not to use  is here anyone who can solve the issue now 


1 ACCEPTED SOLUTION

HI @dheeru_1994 please try to second approach

Script: onSubmit

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 = 1;
	if(window == null){
		// portal
		if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
			alert('You must add 1 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 must add 1 attachments before submitting this request.');
			return false;
		}
	}
}

RamSingh_0-1712748080181.png

 

Please mark reply as Helpful/Correct, if applicable. Thanks!

 

View solution in original post

7 REPLIES 7

Service_RNow
Mega Sage

Hi @dheeru_1994 

(A)Try to this code

function onSubmit() {
   //Type appropriate comment here, and begin script below
   
	var count = getSCAttachmentCount();//Checking attachment count.
	if(count <= 0) {
		alert('You must attach this item');//If there is no any attachment, it will show alert message and abort to submit form. 
		return false;
	}
}

Second Approach

(B) For example: You need to ensure there are exact 1 attachment added before submission

Script: onSubmit

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 = 1;
	if(window == null){
		// portal
		if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
			alert('You must add 1 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 must add 1 attachments before submitting this request.');
			return false;
		}
	}
}

 

 

Isolate Script: Set to False

RamSingh_0-1712720644166.png

Please mark reply as Helpful/Correct, if applicable. Thanks!

 

 

function onSubmit() {
   //Type appropriate comment here, and begin script below
   
	var count = getSCAttachmentCount();//Checking attachment count.
	if(count <= 0) {
		alert('You must attach this item');//If there is no any attachment, it will show alert message and abort to submit form. 
		return false;
	}
}

this is not working error giving 

HI @dheeru_1994 please try to second approach

Script: onSubmit

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 = 1;
	if(window == null){
		// portal
		if(this.document.getElementsByClassName('get-attachment').length != countRequired) {
			alert('You must add 1 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 must add 1 attachments before submitting this request.');
			return false;
		}
	}
}

RamSingh_0-1712748080181.png

 

Please mark reply as Helpful/Correct, if applicable. Thanks!

 

i can not use dom manipulation   now how can i achieve this here