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

I tried this approach, but was unsuccessful, unfortunately.

 

I get this message in the console telling me it cannot find the function I created in the onLoad Script of the variable set.

Error while running Client Script "OnChange: Location": ReferenceError: u_testMe is not defined

Manoj Kumar16
Giga Guru

You can use script includes for this purpose. Write a script include and call the functions from your client script.


HV1
Mega Guru

For shared client scripts you can utilize UI Scripts module:   https://wiki.servicenow.com/index.php?title=Global_UI_Scripts#Global_UI_Scripts&gsc.tab=0


For business rules, use Script Includes to define shared functions and call those from inside business rules: http://wiki.servicenow.com/index.php?title=Script_Includes#gsc.tab=0



- Hardik


Hi Hardik, for UI Scripts, is it necessary to also use a UI Macro?   In the link that you sent above, it sounds like they need to be used together, is this correct?   Or can I create a UI Script and then go into my Record Producer and call upon it by creating a new Client Script with the UI Script function?  



Thanks.


No, UI Macro is not necessary.   You can directly call your UI script from within any client script. Just have to follow these norms:


1.   The UI Script name should be same as the function name defined in it.


2.   You will need to mark the UI script as Global if required to call directly from client script.



Eg:


UI Script:


Screen Shot 2016-11-29 at 8.23.41 AM.png




OnChange Catalog Client Script:


Screen Shot 2016-11-29 at 8.24.20 AM.png



- Hardik Vora