onLoad Client Script

Jimmy Morse
Tera Contributor

I am trying to set-up a client script that will show an error message on a form if a specific field is not populated. Being a novice at scripting I seem to be having some difficulties and not getting it right. This is what I have for my script:

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   if(g_form.getValue.spm_taxonomy_node == null) {


  g_form.addErrorMessage('Service cannot move beyond pipeline phase and approved status until a taxonomy node is set.');
  }
}
 

I have attached a screenshot of the script. Any assistance is appreciated!

1 ACCEPTED SOLUTION

Steven Parker
Giga Sage

The way the script is written now is going to run when the form loads (onLoad script).  Are you sure you want it to run this when the form first loads versus when it's submitted?

 

This will do it if you do want it to run onLoad:

 

function onLoad() {
   //Type appropriate comment here, and begin script below
	var taxonomy = g_form.getValue('spm_taxonomy_node');
	if(!taxonomy) {
		g_form.addErrorMessage('Service cannot move beyond pipeline phase and approved status until a taxonomy node is set.');
	}
}

 

 


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

View solution in original post

14 REPLIES 14

This is a new client script that I created, and I don't have much coding experience,

That you don't have much coding experience, is not important here. What is important, what the actual functional requirement is.

 

That's why I'm asking, though unfortunately you are not answering on this. Else we could have helped you on that also.

 

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

The functional requirement was to display an error message on a form as long as the specific field is not populated. Once it is populated the message disappears. In the test It opens the form with the field blank, making it a requirement to show the error message. The error message was not displayed when the form loaded, so the test failed. So i wrote the onLoad client script, and thanks to you and another gentleman, my rookie coding errors were fixed to resolve my situation. The test now passes with the correct script, I just had some errors in it.

That's why I am asking. Because now we were only able to look at a piece of code and point out wat was wrong. Though actually, knowing the functional requirement, we could have helped you in an other direction. onLoad Client Script is a wrong choice here. Sure it can work, though looking at performance etc, its a wrong choice. Looking at your requirement, this should have been solved Server Side, not Client Side.

 

Kind regards,
Mark

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Steven Parker
Giga Sage

The way the script is written now is going to run when the form loads (onLoad script).  Are you sure you want it to run this when the form first loads versus when it's submitted?

 

This will do it if you do want it to run onLoad:

 

function onLoad() {
   //Type appropriate comment here, and begin script below
	var taxonomy = g_form.getValue('spm_taxonomy_node');
	if(!taxonomy) {
		g_form.addErrorMessage('Service cannot move beyond pipeline phase and approved status until a taxonomy node is set.');
	}
}

 

 


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven