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 Script triggering when page loads or Change advances states.

ZacharyW
Tera Expert

Hello,

I am trying to populate Description and Short Description based upon a list of form choices.

If a user selects "Server Reboot" the following basic information needs to populate.

I have the following script, which populates a these fields when a use changes values. The problem I'm running into is that the value changes when the page refreshes or the change progresses to the next state. Does anyone have any advice?

find_real_file.png

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   
	newValue = g_form.getValue('u_quick_change_type');
	

	
							   
	if (newValue == 'menlo whitelist') {
     
	  
	   
	   g_form.setValue('short_description', 'Menlo Whitelist Add');
	   g_form.setValue('description', 'URL:  \nReason For Change:');
	   
	  
   }
   
	else if (newValue == 'server reboot') {
     
	   g_form.setValue('short_description', 'Server Reboot [Enter Server Name]');
	   g_form.setValue('description', 'Servername:  \nReboot Reason:');
	   
	 
   }
	
	else if (newValue == 'firewall port open') {
     
	   g_form.setValue('short_description', 'Firewall Port Open');
	   g_form.setValue('description', 'Source Port:  \nDestination Port:\nApplication Type:');
	   
	   
   }
	
	else if (newValue == 'firewall port close') {
     
	   g_form.setValue('short_description', 'Firewall Port Close');
	   g_form.setValue('description', 'Source Port:  \nDestination Port: \nApplication Type:');
	   
	  
   }
	
	
}
1 ACCEPTED SOLUTION

Manish Vinayak1
Tera Guru

Add the following line as the first line of your client script for not allowing it to run on load:

 

if(isLoading) {

return;

}

 

View solution in original post

9 REPLIES 9

Allen Andreas
Tera Patron

Just to mention this...when you create an onChange client script (or select it as the "type")...there is default code that is placed in the script section. It looks like this:

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

   //Type appropriate comment here, and begin script below
   
}

Unfortunately, you removed the portion that stops it from executing upon load or also when the newValue for the onchange is empty. Unless you need to remove this part of the code, you really shouldn't alter that.

Simply add it back to have it stop executing upon load.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hello Allen,

I appreciate the feedback. I removed the 

if (isLoading || newValue == '')

line, because it didn't seem like it actually stopped the script from triggering if the page was loading.

|| is the OR symbol, correct? So that line reads: If the page is loading, OR NewValue == Do a thing. Which isn't what I want, because if the page is loading I don't want the line to trigger.

Or am I completely misunderstanding how that line works?

In Javascript, the if statement means the moment any of that is true...it executes the if function. So since isLoading would be met true...on page loading...it executes the

return;

thus ending any further execution contained within. That's why it's there by default.

Anyways, I see you've marked the other response as Correct. so I'm glad you got your answer.

My reply above was more so to explain why. I'm not a big fan of just throwing out the code and going ...here ya go! I try to explain things a bit more so we all can learn (me too).

Take care!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hello Allen,

I appreciate the extra information. Now I have a much better understanding of how the script is triggering. Thanks again!

No problem! 🙂


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!