OnChange variable to make Attachment Mandatory Client Script

Cory Hitchings
Giga Guru

Hi all, 

I have been looking to write an OnChange client script to make an attachment mandatory when a specific variable has changed.  I don't want to use the onSubmit function. 

I also don't want to enable 'Add Attachment' mandatory on the catalog item itself, only when a variable has been changed. 

For example: When 'Supplier Updates' is selected, a new mandatory selectbox appears, if 'Address' is then selected from the next selectbox, another mandatory selectbox appears, if Remit is selected, this is where an attachment becomes mandatory. 

I am not sure how to enable this in the onChange client script.   Does  anyone have any crafty ideas to make this dream a reality?  Thanks!!

find_real_file.png

 

1 ACCEPTED SOLUTION

Now corrected!

//catalog client script to check for attachement
function onSubmit() {
  
	if((g_form.getValue('<variable name>') == 'yes') && this.document.getElementsByClassName('get-attachment').length == 0) {
		g_form.addErrorMessage(getMessage('attachment_is_mandatory'));

		return false;
	}	
}

View solution in original post

10 REPLIES 10

You are amazing, thanks Ian!  This works very well, it's not an onChange script, but does not allow the user to proceed unless an attachment is provided, so really is doing the same thing in a different way.  Much appreciated! 

This was very helpful! I appreciate it. All I needed was to override the standard ServiceNow error message for mandatory attachments and was able to do this with a simplified variation of your script.

function onSubmit() {
   
	if (this.document.getElementsByClassName('get-attachment').length == 0) {
		alert("This request cannot be submitted without adding an attachment. Please use the paperclip to attach, and resubmit.");
		return false;
	}
	
}

Cheers.

@Ian Mildon 

Help..I am in the same boat and trying to get your script to work in our environment and hitting a roadblock

 

We have the following

Access Card request form. If the user selects New Card Request, we want the Attachment to be mandatory

Here are the Variables I am working with

Select Box - Type of Card Requested - Variable - type_of_card_requested
Drop down options
find_real_file.png

For the script you posted I tried to do the following, but it just submitted like normal.  I am sure I am missing some formatting.

//catalog client script to check for attachement
function onSubmit() {

if(g_form.getValue('<type_of_card_requested>') == 'New Card Request') && this.document.getElementsByClassName('get-attachment').length == 0) {
g_form.addErrorMessage(getMessage('attachment_is_mandatory'));

return false;
}
}

 

Any ideas?

AbhishekGardade
Giga Sage

Hello Cory:

you can check out my article on it:

Possible Ways for Making an Attachment Mandatory : Service Portal/Native UI

If you are in or after madrid release then:

Option to make the attachment mandatory for catalog items.

 Hide Attachments/Mandatory Attachments

Please mark as Correct Answer and Helpful, if applicable.
Thanks!
Abhishek Gardade
Hexaware Technologies

Thank you,
Abhishek Gardade

Thank you for the response Abhishek.  I do not want attachments mandatory for the entire catalog item, rather only 'if' the variable is selected.