- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2016 07:22 AM
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!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
-
Team Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2016 08:51 AM
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:
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2021 12:47 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2016 07:28 AM
You can use script includes for this purpose. Write a script include and call the functions from your client script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2016 07:30 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2016 08:41 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2016 07:27 AM
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:
OnChange Catalog Client Script:
- Hardik Vora