Display message based on selection in Catalog Item variable?

JR42
Giga Guru

Hello!  I would like to display a message to the user when they select a specific choice in one of the variables.

As soon as they choose that choice, the message should be displayed. 

The variable name is: would_you_like_to_add_update_or_remove_a_patching_record

The choice that should prompt the message is: update

 

I've tried the following code (and some others) on the Catalog Client Script but it is not displayed the message.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  if (!isLoading && control.name === 'would_you_like_to_add_update_or_remove_a_patching_record') {
    if (newValue == 'update') {
      g_form.showFieldMsg('would_you_like_to_add_update_or_remove_a_patching_record', 'Update the fields that you would like updated, leave the field blank if no update is required.', 'info');
    } else {
      g_form.hideFieldMsg('would_you_like_to_add_update_or_remove_a_patching_record');
    }
  }
}

 

2023-02-23 09_29_31-Message for Update request on Patching _ Catalog Client Scripts _ ServiceNow [DE.png

Hoping someone can assist with the script.

 

Thank you!

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Hi Justin,

You can troubleshoot this by adding an alert to see how the if statements are evaluating.  There's no point in adding control.name in the first if, so I'd go back to the standard format like this:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
    	return;
   	}

	if (newValue == 'update') {
		alert('update')
		g_form.showFieldMsg('would_you_like_to_add_update_or_remove_a_patching_record', 'Update the fields that you would like updated, leave the field blank if no update is required.', 'info');
    } else {
        alert('newValue='+newValue)		
        g_form.hideFieldMsg('would_you_like_to_add_update_or_remove_a_patching_record');
    }
}

This will show the value of newValue if it's not 'update' - if you're expecting that, maybe it's 'Update' or something else.  In the future, do yourself a favor and keep the Catalog Item variable names short!

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

Hi Justin,

You can troubleshoot this by adding an alert to see how the if statements are evaluating.  There's no point in adding control.name in the first if, so I'd go back to the standard format like this:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
    	return;
   	}

	if (newValue == 'update') {
		alert('update')
		g_form.showFieldMsg('would_you_like_to_add_update_or_remove_a_patching_record', 'Update the fields that you would like updated, leave the field blank if no update is required.', 'info');
    } else {
        alert('newValue='+newValue)		
        g_form.hideFieldMsg('would_you_like_to_add_update_or_remove_a_patching_record');
    }
}

This will show the value of newValue if it's not 'update' - if you're expecting that, maybe it's 'Update' or something else.  In the future, do yourself a favor and keep the Catalog Item variable names short!

Paul Porter
Tera Expert

@Brad Bowman  , this looks like it may be useful to me also. Before I dive in, can you tell me if this code will work in the Portal too? 

 

Thanks,

~Paul