--- title: "Writing a Function to use in Scripts and Expressions" slug: "writing-a-function-to-use-in-scripts-and-expressions" updated: 2024-03-28T17:14:25Z published: 2024-03-28T17:14:25Z canonical: "skthelp.syniti.com/writing-a-function-to-use-in-scripts-and-expressions" --- > ## Documentation Index > Fetch the complete documentation index at: https://skthelp.syniti.com/llms.txt > Use this file to discover all available pages before exploring further. # Writing a Function to use in Scripts and Expressions Even though you can use the full range of C# or VB .NET syntax when writing a Syniti Replicate script, there are times when you need to write a custom function to use in scripts or expressions. You can use the [Global Script Editor](/v1/docs/global-script-editor) to write and compile the function, then call it from a replication script or expression. Here is an example in VB.NET of how you might write and use a function that trims values and accepts null values: ```plaintext Public Shared Function SafeTrim(objString as Object) as Object   'If the parameter is null return a null value   If objString Is Nothing Then      Return System.DBNull.Value      End If   Return Trim(objString.ToString()) End Function ``` 1. In the Management Center Metadata Explorer, select the metadata where you want to use the function. 2. From the right mouse button menu, choose **Global Script**. The Global Script Editor opens to display a stub: ```plaintext Imports Microsoft.VisualBasic Imports DBMotoPublic Imports DBMotoScript Namespace DBRS    Public Class GlobalScript : Inherits IGlobalScript    End Class    Public Class MappingRule : Inherits IMappingRule    End Class    Public Class GlobalEvents : Inherits IGlobalEvents    End Class End Namespace ``` 1. Type your function between the "Public Class GlobalScript : Inherits IGlobalScript" line and the corresponding "End Class" line so that it looks as follows: ```plaintext Imports Microsoft.VisualBasic Imports DBMotoPublic Imports DBMotoScript Namespace DBRS    Public Class GlobalScript : Inherits IGlobalScript         Public Shared Function SafeTrim(objString as Object) as Object           'If the parameter is null return a null value           If objString Is Nothing Then Return System.DBNull.Value             Return Trim(objString.ToString())         End Function    End Class    Public Class MappingRule : Inherits IMappingRule    End Class    Public Class GlobalEvents : Inherits IGlobalEvents    End Class End Namespace ``` 1. If you have added any libraries to the list of imports in the script, click ![](https://cdn.document360.io/ca09b4e9-1857-4c63-9f0e-1f25f166f447/Images/Documentation/ScriptsEditorReferences_29x28.png?sv=2026-02-06&spr=https&st=2026-11-17T19%3A22%3A27Z&se=2026-11-17T20%3A22%3A27Z&sr=b&sp=r&sig=Z4ZBs82I2wD7HVp3EawwgtNGaJyZWnFRl1H%2FlvJsWt8%3D) to open the References dialog and add the path to the library. 2. Click **Compile** ![](https://cdn.document360.io/ca09b4e9-1857-4c63-9f0e-1f25f166f447/Images/Documentation/ScriptsEditorReferences_25x25.png?sv=2026-02-06&spr=https&st=2026-11-17T19%3A22%3A27Z&se=2026-11-17T20%3A22%3A27Z&sr=b&sp=r&sig=n%2BYUItDubyorCVcfj6DolWy8OapUd2D5Ul%2BA4XL%2FLlY%3D) to check your script syntax. Any script problems are displayed in a separate dialog. 3. Correct any syntax errors. Now you can use the function from a replication script or expression. The steps below show you how to use a function in an expression. 1. If you are using the function in an existing replication, either stop the Replication Agent or disable the replication. 2. For an existing replication, right-click on the replication and choose **Replication Properties**, then click **Mapping** to display the Mapping Editor. For new replications, perform the following steps when you get to the mapping portion of replication creation. 3. In the left hand pane of the Mapping Editor, right-click on each field that will be used as a parameter for the function and make sure **Use Unmapped** is checked. If it is not checked, select the menu item to check it. 4. Right-click on the target field that will be using the generated value and choose **Map to Expression**. 5. In the Expression Generator window, expand the User Functions folder and click on (all) to see if your function is listed. If the function is not displayed, check to make sure that you have used the proper Public Shared qualifier for the function. See step 3 above for more information. 6. Double-click on the function name to use it in the Expression Editor. 7. Either type in the names of the fields you want to use in the function or expand Values, then Fields and double click on each field name to add it to the Expression Editor. Note that field names should be enclosed in square brackets [] as in the example below. In this case, the value of the field MyText is being passed to the function SafeTrim (defined in step 3 above). ```plaintext         SafeTrim([MyText]) ``` 1. Click **OK** to close the Expression Generator window. 2. Click **OK** to close the Mapping window. 3. Click **OK** to close the Replication Properties window. 4. Either start the Replication Agent or enable the replication to test that your function is working. **Related Topics** [Global Script Function Examples](/v1/docs/vb-net-global-script-function-examples) [Writing a Global Script](/v1/docs/writing-a-global-script)