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.
UI Macros #
Include the code in a UI Macro and reference that in the Catalog Item.
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.