Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Ian Mildon
Tera Guru

While this script is onSubmit, it should be able to be modified to an onChange; just modify the getValue to be the variable and value you want as the trigger.

//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;
	}	
}

Thank you very much Ian, looks like it's super close, but getting errors on the script: 

function onChange() {

if(g_form.getValue('please_select_one_supplier_address') == 'remit') && this.document.getElementsByClassName('get-attachment').length == 0) {
g_form.addErrorMessage(getMessage('attachment_is_mandatory'));

return false;
}
}

 

find_real_file.png

I noticed that I have a typo on the IF line; missing parenthesis. Trying to figure out where it's missing from.

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;
	}	
}