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 DR 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 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:

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:

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:

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 to open the References dialog and add the path to the library.

  2. Click Compile 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).

        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

Writing a Global Script