Reusing Client Scripts

yundlu316
Kilo Guru

Our team is creating a number of record producers that utilize the same Client Scripts and Business Rules.   As of right now, we are just copying and pasting them individually into each record producer, but is wondering if there's a better way to do this.   The way we're currently doing this could be a pain to update or maintain in the future.   Is there a way to use UI Macros or something similar that would save the code and we can just call upon it in each of our record producers?   That way, if changes in the code arise, we wouldn't have to update the code in each record producer.

Thanks!

1 ACCEPTED SOLUTION

I don't know the details of your requirements, but this is how you can create shareable code for catalog items, including Record Producers:



1. Create a new Variable Set


2. Create a new onLoad Client Script as part of that Variable Set and include the following code:



function onLoad() {


    //this is just a placeholder, but is required


}



function u_sharedFunction1(){


  //add your code here


  alert("You called?");


}



function u_sharedFunction2(){


  //add your code here


}



The onLoad() snippet is just a placeholder and does not need to contain anything, but is nonetheless required to be there.   The u_sharedFunction1 and 2 are whatever shared functions you have.   Add as much as you need.   Adding them to the onLoad script makes them available within the Record Producers.



3. Add the Variable Set to whatever Record Producers needs it


4. Call one of the functions from a Client Script in one of your Record Producers.   Example:



function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  return;


  }



  u_sharedFunction1();


}



A "You called?" alert would appear on screen in the example above:


find_real_file.png




Now you have simple, shareable AND easily managed code available to your Record Producers without the overhead of Global UI Scripts.   The code is only loaded for those catalog items you add the Variable Set to.


View solution in original post

19 REPLIES 19

It actually has to be an onLoad script in order to work.


Hi Jim, do you know if this method still works in London? I am getting a similar error to peterraeves, except in the platform as well.

You can create a UI Macro variable in the variable set to contain the shared code:

<?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 uiMacroSharedCode() {
			alert("Running from the UI Macro");
		}
	</script>
</j:jelly>

 

find_real_file.png

 

BUT, that only works in the Desktop UI and not the Portal.  The Portal does not render UI Macros.  You would have to also created a Widget and associate it with the variable in order for the Portal to load it.  Not sure what the Widget would contain though.  Will try to look at that later.

Actually, in my example I am wanting to do have a reusable function for client scripts on the incident table. It's not a catalog item at all. So do I need to add a formatter to the form with this Jelly stuff?

When I do this on the portal, I get 'Error while running Client Script "": ReferenceError: my_function is not defined'. Both client scripts run on portal, the function just is not accessible any more. It works in service catalog. any idea why?