You are viewing an older version of this section. Click here to navigate to the latest version.

Output Fields Assigned Scripts

Version 3.3.1 only

As detailed in the Anypoint DataMapper Transformer Reference page, Anypoint DataMapper uses the CTL2 transformation language to transform data between mapped fields. Mule uses the term Assigned Script to refer to the set of CTL2 statements that defines a data mapping transformation.

While DataMapper provides a graphical interface for editing data maps, it also enables you to view and edit the CTL2 statements that define data transformations. This document explains the different options that DataMapper provides for viewing and editing the Assigned Script.

This document assumes the reader is familiar with the DataMapper Transformer. Consult the DataMapper Transformer Reference document for details.

Viewing An Output Field’s Assigned Script

To view an output field’s assigned script, click the field name in the DataMapper’s Output pane. DataMapper displays the field’s assigned script in the textbox under the Output pane. For example, the assigned script for the output field name is the CTL2 statement $; (see image below).


Note that under the textbox for the assigned script, DataMapper displays the message, The assigned script is valid. DataMapper evaluates a script the moment you map data (i.e. when you define a mapping, or immediately after you create or edit an assigned script). DataMapper evaluates the script for both syntax and content. If the script returns an error, DataMapper displays a diagnostic under the script’s textbox (see syntax error in image below, left; content error in image below, right).


Editing An Assigned Script

There are two ways to edit the script assigned to a field:

  • edit directly in the Script Textbox, below the output pane, if you are familiar, and comfortable working with CTL2 functions

  • edit via the Expression Editor if you wish to access a library of CTL2 functions to help you build your script

Editing In the Script Textbox

  1. In the Output pane, click the field name whose script you wish to edit.

  2. Edit the script directly in the Script Textbox below the output pane.

  3. After editing, you can perform one of two actions to trigger DataMapper’s script evaluation:

    • press enter

    • click another field in the output pane

Editing in the Expression Editor

  1. Click the Expression Editor icon to the right of the script textbox (see image below, top) to open the Expression Editor panel (see image below, bottom). Alternatively, double-click the field whose script you wish to edit.



    • 1 - the Expression pane, which displays all the mapping’s input fields

    • 2 - the Function pane, which displays CTL2 functions that you can use in the assignment script

    • 3 - the Script pane, which contains the assignment script

    • 4 - the Filter textbox which displays filtered content in the Function pane according to the string you enter in the text box

    • 5 - Operators you can insert into the script, such as logical OR, logical AND, etc.

  2. Edit the script directly in the Script pane.

  3. Alternatively, click to drag any input field from the Expression pane or any function from the Function pane into the Script pane. (Refer to the Using Functions example below for an illustration of this activity.)

Example: Combining Two Input Fields

In this example, we first combine the contents of the name and firstname input fields to create a new output field, fullname, then we add a space between the input fields to format the output.

  1. Right-click the top-level element — in this case, emergencyContacts — then select Add Field.


  2. In the New Attribute window, define parameters for the new output field:

    • Type: Attribute

    • Name: fullname

    • Configuration Type: string


  3. Click OK to save the new field.

  4. In the graphical mapping editor, map the name and lastname input fields to the fullname output field.


  5. Click the fullname output field to see the script that DataMapper assigned to the field, $ + $in.0.lastname; (see image below).


  6. Click the DataMapper Preview tab (below the Input pane) to view the assigned script. Note that there is no space between the first and last names.


    For more details on using DataMapper’s Preview feature to test your mapping, consult Obtaining a Preview of Mapped Data.
  7. Click the Mappings tab to return to the main mapping pane, then edit the assigned script in the Script Textbox to add a white space between the first and last names: $ + '' + $in.0.lastname;


  8. Click the DataMapper Preview tab to view the adjusted script. Note the space between the first and last names.


Example: Using Functions

In this example, we use a script to split an input field into two output fields. The script uses CTL2’s substring function to extract specific bytes from the input field. The input field, phone, is a string containing eleven digits. The first three digits are the area code; the remaining are the phone number itself.

The mapping preview of this example displays phone numbers, each as one long string of numbers (see image below).


To split the contents of the phone input field into two output fields — areacode and phone — we must complete the following three tasks:

  1. Assign a script to the phone output field that returns the last eight digits of the phone input field.

  2. Create the output field areacode.

  3. Assign a script to the new areacode field that returns the first three digits of the phone input field.

We use the CTL2 substring function in the assigned scripts of each output field. The following procedure describes how to enter this function using the Script Textbox or the Expression Editor.

  1. Use one of the following two embedded procedures to assign a script to the phone output field that returns the last eight digits of the phone input field.

    1. Click the phone field in the Output pane.

    2. In the Script Textbox, delete the contents, then enter substring($, 3,8);

    3. Press enter to initiate DataMapper’s script evaluation (see image below).


Via Expression Editor
  1. Click the Expression Editor icon to open the Expression Editor panel.

  2. In the Filter field, type string to contract the list of functions Mule displays in the Function pane to only those that contain the word "string".

  3. Hover your mouse over the function labelled string substring(string, integer, integer) to display a description of the function (see image below).


  4. Click, then drag the string substring(string, integer, integer) function into the Script pane to automatically generate the CTL2 code for that function (see image below).


  5. In the Script pane, add content to the script to define the requirements of the example. In this case, edit the script to read, substring($, 3,8) (refer to image below).


  6. Click OK to save your script changes and close the editor.

    1. To create the new areacode output field, right-click emergencyContacts, then select Add Field.

    2. In the New Attribute panel, define the parameters of the new field as follows:

      • Type: Attribute

      • Name: areacode

      • Configuration Type: string

    3. Click OK to save the new output field.

    4. In the Script Textbox, type the function that returns the first three numbers of the phone input field: substring($, 0,3);.

    5. Press enter to initiate DataMapper’s script evaluation.

    6. Click the Preview tab to review DataMapper’s newly configured output (see image below).