Script Libraries for Code Reuse
Overview
This guide describes how to organize and reuse code by moving shared logic into script libraries.
By placing shared code in script libraries, you can reduce redundancy, improve maintainability, and ensure consistency across multiple XDM objects, such as:
-
Modification Methods,
-
Task Stage Hooks,
-
Workflows.
Steps
Step 1: Create a Script Library
A script library is a reusable collection of code placed in a dedicated file within the XDM configuration objects.
Script libraries allow you to centralize code that is used in multiple places.
To create a script library:
-
Create a new XDM file under XDM configuration → Files.
-
Assign a name to the file that reflects its functionality.
-
Choose the script language that matches the language used in the code you want to reuse.
| Script libraries can only be used in scripts that are written in the same script language. |
Step 2: Move Shared Code to the Script Library
After the script library file has been created, move the shared code into the script library.
This code may come from objects such as:
-
Modification Methods
-
Task Stage Hooks
-
Workflows
To move shared code:
-
Identify the code sections that should be reused.
-
Create functions in the script library that contain this logic.
-
Replace the original code with calls to the functions from the script library.
Example
Original code in a Modification Method:
def apply() {
data[columnIndex] = data[columnIndex].replace(" ", "")
}
This code removes spaces from a value in the data array.
To move this logic to a script library, wrap it in a function.
Code in the script library file:
def removeSpaces(value) {
return value.replace(" ", "")
}
Modified code in the original object:
def apply() {
data[columnIndex] = removeSpaces(data[columnIndex])
}
Step 3: Reference the Script Library in the Original Object
To use the shared functions from the script library, reference the script library file in the object where the functions are needed.
To reference the script library:
-
Open the configuration of the object, for example a Modification Method, Task Stage Hook, or Workflow.
-
Locate the Libraries section.
-
Add a reference to the script library file created in Step 1.
Step 4: Test and Validate the Changes
After integrating the script library, test the affected objects.
Verify that:
-
the shared code is executed correctly
-
no script errors occur
-
the Modification Methods, Task Stage Hooks, or Workflows behave as expected
-
the script library is referenced in all required objects
Benefits
Using script libraries provides the following benefits:
-
Reusability: Reuse the same code across multiple locations without duplicating it.
-
Maintainability: Manage shared logic in one place, making updates easier.
-
Consistency: Ensure that the same logic is applied consistently across different objects.
-
Cleaner code: Keep Modification Methods, Task Stage Hooks, and Workflows focused on their main purpose.
-
Reduced errors: Avoid inconsistencies caused by maintaining duplicate code fragments.
Conclusion
Script libraries provide a clean and efficient way to manage shared code in XDM.
By moving frequently used logic into script libraries, you can centralize code, reduce redundancy, and simplify maintenance.
This approach is especially useful when the same logic is used in multiple Modification Methods, Task Stage Hooks, or Workflows.