How to hide Form Annotation until specific field condions are met

Jerri
Mega Contributor

I am trying to add a form annotation that displays only if certain field conditions are met. Can anyone point me in the right direction?

1 ACCEPTED SOLUTION

Change your client script to run 'onChange' against the 'Business service' field.  Then use the script below (adjusting the Business Criticality values to match yours as needed) and you should be good to go.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	// Get the 'Business service' reference
	var bs = new GlideRecord('cmdb_ci_service');
	bs.addQuery('sys_id', newValue);
	bs.query(showBSAnnotation);
}

function showBSAnnotation(bs) {
	if (bs.next()) {
		// Show annotation if high business criticality.
		if ((bs.busines_criticality == '1 - most critical') || (bs.busines_criticality == '2 - somewhat critical')) {
			// Hide the annotation
			$('my_annotation').up().show();
		}
		else {
			// Hide the annotation
			$('my_annotation').up().hide();
		}
	}
	else {
		// Hide the annotation
		$('my_annotation').up().hide();
	}
}

Please mark this response correct if I've answered your question.  Thanks!

View solution in original post

26 REPLIES 26

Mark Stanger
Giga Sage

This is fairly simple to do with annotations on a standard form.  The first thing you need to do is construct your annotation in a specific way...using an HTML annotation with a uniquely-identifiable container element that you can show and hide with your script.  Something like this should work...

find_real_file.png

Here is the annotation text with HTML as shown in the screenshot for easier copying...

<span id="my_annotation">Here is my annotation message text!</span>

Once you've got your annotation set up correctly you can use DOM manipulation in a client script or UI policy script to target the element and show and hide it whenever you want.  Generally DOM manipulation is frowned upon because you can't control what ServiceNow does with the HTML of forms in the future, but in this case, the risk is much lower since you control the ID.

Here's an example of a working client script to show and hide the annotation above based on changes to the category value on an incident.  If you're on a London ServiceNow release note that you need to un-check the 'Isolate script' checkbox so that your DOM manipulation script can run without issue.

find_real_file.png

Here's the script above for easy copying...

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	// Hide annotation if 'category' is 'hardware'. Show otherwise
	if (g_form.getValue('category') == 'hardware') {
		// Hide the annotation
		$('my_annotation').up().hide();
	}
	else {
		// Show the annotation
		$('my_annotation').up().show();
	}
	
}

Please mark this response correct if I've answered your question.  Thanks!

Other than the fact that I had to add an onLoad script to hide the annotation at form load, This is perfect!

I tried using this method from within a scoped app, doesn't work. Is there something I need to consider?

Thanks

I got this work, apparently scoped apps client scripts do not have the same exposure to certain APIs as global. added a system property and worked easy

https://community.servicenow.com/community?id=community_blog&sys_id=788c66e1dbd0dbc01dcaf3231f961969

 

Thread is very helpful