How To Reuse Client Side Script Code

David165
Mega Expert

Hi

I've written a short client script to change the colour of a selection value form field when the value changes. It works well, but I want to use the same code across quite a number of form fields. How can I put this code into a client side function that I can call for each of my form fields?

I looked at UI scripts, but my head exploded with the complexity of implementing these. Isn't there a simpler way to call a custom client side function?

Here's the onChange function I'd like to call for a variety of form fields:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	var elementID = g_form.getElement(control.name);
	
	switch(newValue) {
		case "3": elementID.style.backgroundColor = "red"; break;
		case "2": elementID.style.backgroundColor = "orange"; break;
		case "1": elementID.style.backgroundColor = "yellow"; break;
		default: elementID.style.backgroundColor = "white"; break; }
		
}

Thanks in advance.

David

1 ACCEPTED SOLUTION

David165
Mega Expert

Hi

I'm delighted to report that I have, at last, resolved this issue. After weeks of trying different methods to load the functions I wanted to share between fields on a form (so I could call the same functions onChange on several form fields) I could get it to work by including my shared functions within <script></script> tags inside a UI Macro:

<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
	<script>
		function calcgxpassessment() {

                  // My code here
		}
	</script>	
</j:jelly>

Within my code, I had to rework things like && and change double quotes because these are XML entities. I haven't tried replacing these with the correct XML entities (E.g replacing && with &amp;&amp;)

I then included the UI Macro in a UI Formatter

find_real_file.png

and then dropped the UI formatter into the bottom of the form.

Finally, I was able to call the function calcgxpassessment() onChange from any choice field that needs to recalculate the risk. I also had to remember to deselect "Isolate script" from these client scripts. Otherwise, they couldn't see the shared function.

My project is back on the rails ðŸ™‚

Regards

David

 

View solution in original post

18 REPLIES 18

Elijah Aromola
Mega Sage

You could write a script include and call it with a Glide Ajax call. You could also write a function in an onLoad script but outside of the actual onLoad function like: 

function onLoad() {
     //Type appropriate comment here, and begin script below
}

function yourFunctionHere() {
   //Do stuff
}

 

Then call the function within your onChange script. 

Hi Elijah

Thanks for your reply. I tried the onLoad suggestion as it seems the simpler of the two, but unfortunately this doesn't work. When the form is loaded, and the onChange fires for the form fields, I get this error:

 

onChange script error: ReferenceError: setColour is not defined function() { [native code] }

 

It appears that the onLoad hasn't loaded my function at the time the onChange is called?

This is my onLoad script:

function onLoad() {
	// This function does nothing, but the code below gets loaded.
	
}

//
// Set the colour of a form field according to the value selected.
//
function setColour(control, newValue) {
	var elementName = control.name;
	var elementID = g_form.getElement(elementName);
	
	switch(newValue) {
		case "3": elementID.style.backgroundColor = "red"; break;
		case "2": elementID.style.backgroundColor = "orange"; break;
		case "1": elementID.style.backgroundColor = "yellow"; break;
		default: elementID.style.backgroundColor = "white"; break; 
	}
	
}

And this is now my onChange script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	setColour(control, newValue);
		
}

You have to set

 find_real_file.png

to false (unchecked). If you don't see it at the bottom of your script, go to form layout and add it to the bottom of your form and uncheck it.

 

Please mark this as helpful/correct if it resolved your issue!

Community Alums
Not applicable
Make sure the script include is 'Client Callable'