Custom JavaScript Transformations on Virtual Data Resources

Why Use Custom JavaScript? 

Custom JavaScript can be used to manipulate fields from a transformation object to fit the structure and format of a virtual data resource in ways that cannot be achieved through mapping. Suppose that your transformation's contact object has the individual fields firstName and lastName yet your virtual data resource uses a single field, fullName. Custom JavaScript can be used to combine the values for firstName and lastName to populate your virtual data resource's fullName field with a singular value for the contact's name. 

How to Add Custom JavaScript

Note: This article assumes that you already have created a virtual data resource and at least one transformation for that resource. For more information on, see Creating virtual data resources.

To get to a virtual data resource:

  1. Access the page.
  2. Select a resource from the My Resources list.
  3. Open one of your transformations from the Mapped Transformations pane on the right side of the screen. 
  4. Click Custom JS in the upper-right corner to open a code editor where you can insert your custom JavaScript. 

Common JavaScript Transformations

Below are some code samples for common JavaScript transformations. You can find more examples in the Examples section of Transforming Fields. In all of these code samples, data from the originalObject argument is used to create new fields on or otherwise modify the transformedObject argument. When you have finished modifying/creating your transformedObject, you will need to call done(transformedObject); at the end of your JavaScript. 

To test the results of your JavaScript, you can use click the play icon button which will show you the request being made along with the original and transformed object. You can also test the transformation using a specific object by using the ID field and rerunning the test. 

Combining Fields

transformedObject.fullName = `${originalObject.firstName} ${originalObject.lastName}`;

Dividing Fields

let splitName = originalObject.fullName.split(' ');

	transformedObject.firstName = splitName[0];

	transformedObject.lastName = splitName[1];

Replacing Unwanted Characters in Fields

transformedObject.fieldWithoutNewLines = originalObject.field.replace(/\r?\n|\r/g, "");