SAP Cloud Platform Open Connectors

Custom JavaScript Transformations on Common 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 common 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 common resource uses a single field, fullName. Custom JavaScript can be used to combine the values for firstName and lastName to populate your common 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 common resource and at least one transformation for that resource. For more information on, see Creating common resources.

To get to a common resource:

  1. Access the
  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, "");