Table of Contents

Custom script types

Custom Script Types in DynamicWeb Integration enable advanced data transformation during column mapping. By implementing a Script Type Provider, developers can inject custom logic to manipulate or derive values from the source before they're mapped to the destination system.

What is a Script Type Provider?

A Script Type Provider is a class that extends the integration mapping logic by overriding the value assignment for a specific column. It offers access to:

The key method to override is:

GetValueTyped(object? input)

This method receives the input from the source system and returns the transformed output that will be written to the destination.

Implement a Script Type Provider

To create a custom Script Type, inherit from the ScriptTypeProvider<T> class and override the GetValueTyped method. Here's a simple example:

[AddInLabel("Custom Script")]
public class CustomScriptType : ScriptTypeProvider<string>
{
    public override string GetValueTyped(object? input)
    {
        // Your transformation logic here
        return input?.ToString() ?? string.Empty;
    }
}

Examples

1. Constant Value Provider

Returns a fixed value for every row, ignoring the input.

[AddInLabel("Constant")]
public class ConstantScriptType : ScriptTypeProvider<string>
{
    public override bool DisableSource => true;

    [AddInParameter("ScriptValue"), AddInLabel("Script value"), AddInParameterGroup("Scripting"), AddInParameterEditor(typeof(TextParameterEditor), "textArea=true")]
    public string ScriptValue { get; set; } = "";

    public override string GetValueTyped(object? input) => ScriptValue;

    public override string ToString() => ScriptValue;
}

2. Append Text to Input

Appends a predefined string to the source input.

[AddInLabel("Append")]
public class AppendScriptType : ScriptTypeProvider<string>
{
    [AddInParameter("ScriptValue"), AddInLabel("Append text"), AddInParameterGroup("Scripting"), AddInParameterEditor(typeof(TextParameterEditor), "")]
    public string ScriptValue { get; set; } = "";

    public override string GetValueTyped(object? input) => $"{input}{ScriptValue}";
}

3. Substring Extractor

Extracts a portion of the input string.

[AddInLabel("Substring")]
public class SubstringScriptType : ScriptTypeProvider<string>
{
    [AddInParameter("StartIndex"), AddInLabel("Start index"), AddInParameterGroup("Scripting"), AddInParameterEditor(typeof(NumberParameterEditor), "")]
    public int StartIndex { get; set; }

    [AddInParameter("Length"), AddInLabel("Length"), AddInParameterGroup("Scripting"), AddInParameterEditor(typeof(NumberParameterEditor), "")]
    public int Length { get; set; }

    public override string GetValueTyped(object? input)
    {
        var inputString = input?.ToString() ?? string.Empty;
        if (StartIndex < 0 || Length < 0 || StartIndex + Length > inputString.Length)
            return inputString;

        return inputString.Substring(StartIndex, Length);
    }
}

4. Product Number to Product ID

Looks up and returns a ProductId based on a provided ProductNumber.

[AddInLabel("Product Number to ID")]
public class ProductNumberToIdScriptType : ScriptTypeProvider<string>
{
    public override string GetValueTyped(object? input)
    {
        var productNumber = input?.ToString();
        if (string.IsNullOrEmpty(productNumber))
            return string.Empty;

        var product = Services.Products.GetProductByNumber(productNumber);
        return product?.Id ?? string.Empty;
    }
}
Warning

Performance Warning: GetValueTyped is called once per row during integration. Avoid expensive operations (e.g., repeated database lookups) to maintain performance. Consider caching or batching when necessary.

Best Practices

  • Optimize for Performance: Avoid costly operations inside GetValueTyped. If needed, implement caching or preprocessing strategies.
  • Ensure Error Resilience: Validate input values and handle exceptions gracefully.
  • Maximize Reusability: Use parameters to make scripts flexible and usable across multiple scenarios.
  • Label Clearly: Use meaningful AddInLabel and parameter names to help users understand each script's purpose.

By using Custom Script Types, you gain fine-grained control over your data flows, making DynamicWeb integrations smarter and more adaptable to your unique business needs.

To top