Reusing client script code

There are a number of techniques which can be used in ServiceNow to re-use client script code. It is important to be well aware of these, as it is highly tempting for developers to copy and paste code across different client side scripting elements. This is especially so when building up our ServiceNow catalog, where it is likely that many catalog items will need to include similar or identical client side logic.

If we are not careful to enforce code re-use from day one, we can easily arrive at a situation where maintenance of this logic becomes unmanageable due to the large number of copies of the same block of code.

Below you can find a description of these techniques. Choose whichever one fits your needs best, but remember that copying and pasting code is never a good solution for any problem.

UI Scripts

Leverage the UI Scripts functionality included in ServiceNow to share client-side code.

https://docs.servicenow.com/bundle/london-application-development/page/script/client-scripts/concept/c_UIScripts.html

https://docs.servicenow.com/bundle/london-application-development/page/app-store/dev_portal/API_reference/GlideUIScripts/concept/GUIScriptsAPI.html#GUIScriptsAPI

UI Macros

Include the code in a UI Macro and reference that in the Catalog Item.

https://community.servicenow.com/community?id=community_question&sys_id=84134fa1dbd8dbc01dcaf3231f961990

Variable Sets (Catalog-specific)

Write your common code in a Variable Set, and associate that Variable Set to all the Items which require the shared functionality.

https://community.servicenow.com/community?id=community_question&sys_id=890a4369db5cdbc01dcaf3231f961903

What's here

Last modified on Nov 29, 2019