Attachment to be attached if the user Checks the checkbox

Balaraju K B
Tera Expert

Hi Team,

I have a check box in my catalog item

  • If the user checks that check box then he must attach an attachment
  • If the user not checking that check box in this case he can submit the catalog item without attaching.

Please let me know how to get this done?

@Ankur Bawiskar 

 

Thanks in Advance.,

Balaraju K B

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Balaraju K B 

this onSubmit client script should help

1) Ensure the Isolate Script field is set to false for this client script

2) By default when you create new client script it is true

Note:

Ensure you give valid checkbox variable name and your message for alert/form message

function onSubmit() {
	//Type appropriate comment here, and begin script below

	if(g_form.getValue('checkbox_variable').toString() == 'true'){
		if(window == null){
			// portal
			if(this.document.getElementsByClassName('get-attachment').length == 0) {
				g_form.addErrorMessage('You must add attachment before submitting this request.');
				return false;
			}
		}
		else{
			// native view
			var length = $j("li.attachment_list_items").find("span").length;
			if(length == 0){
				g_form.addErrorMessage('You must add attachment before submitting this request.');
				return false;
			}
		}
	}
}

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

15 REPLIES 15

@Balaraju K B 

Can you check if it is working on portal or not?

can you update as below and check for the else part

else{
            // native view
            var attachments = document.getElementById('header_attachment_list_label');
            if (attachments.style.visibility == 'hidden' || attachments.style.display == 'none'){
                g_form.addErrorMessage('You must add attachment before submitting this request.');
                return false;
            }
        }

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Balaraju K B 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate answer as correct & helpful to close the thread.

If not, please let us know if you need some more assistance.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Balaraju K B

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark my response as correct & helpful so that this thread can be closed and others can be benefited by this.

If not, please let us know if you need some more assistance.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Balaraju K B

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark my response as correct & helpful so that this thread can be closed and others can be benefited by this.

If not, please let us know if you need some more assistance.

Regards
Ankur

 

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I used this in a condition when I enter a domain that ends with the word 'com' in a single text input field, then the checkbox should be visible (implemented this using UI policy) and when the checkbox is true then the attachment is mandatory (implemented using the code you gave).

Say for instance if I enter the domain 'example.com' and then the checkbox is visible. If I don't attach something, I get the error as per the code and then I change the domain as 'example.co.uk'. If I submit now, I still get the error. 

Please help on how to solve this.