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

Global UI Scripts are really not the best idea - they are loaded by every single page.   They can be useful in a limited number of scenarios, but they should be avoided as much as possible, especially when better solutions are readily available.


I agree.   However per Dave's requirement to have a shared client function without using a UI Macro, i thought this can be an one-off option to consider.    


For non-global ui script, a UI macro will be necessary.



- Hardik


Mujtaba Amin Bh
Mega Guru

Yes, you can create a UI Macro and then reuse it.



Please see below link:



Reusing Client Scripts without Global UI Scripts


Kalaiarasan Pus
Giga Sage

How to achieve re-usability with ServiceNow?



If the business rule uses same kind of code, it is better to place the code in a script include and access it in any business rule required.


If the code needs to be shared among service catalogs, I would place them in a variableset and add it in any catalog required. You can even place individual client side functions in client scripts and call it.


If code is required on client side at table level, I will place the functions inside any onload client script and access them on any client side object (UI policy, client script, etc) of the form



Global UI script is a bad practice and has a performance hit. Global UI scripts can be useful in some cases but I have always found ways to avoid them.


> If the code needs to be shared among service catalogs, I would place them in a variableset and add it in any catalog required.

 

Do you think it still works in Jakarta? I've heard ServiceNow restricted Portal security...

In particular, my function is not visible (typeof returns undefined) from within onChange on Catalog Item, depite the function is declared properly in onLoad of the same Catalog Item and onLoad is being called while loading. Same negative result when I define the function in onLoad Client Script in a variable set and add this set to my Catalog Item.