Useful script expressions

Good to know

In your script, you can only work with properties that are explicitly defined in your selected definitions. If you made any mistakes the script will not compile and cannot be saved.

This ensures:

  1. Avoiding Mistakes: You can't accidentally reference a property that doesn't exist due to typos or incorrect assumptions.

  2. Data Integrity: You only work with valid properties, ensuring your script behaves as expected and avoids runtime errors.

Always test your scripts!

Script expressions


Here are some ideas for what you can do with scripts; the possibilities are endless. Thanks to Intellisense you can immediately see if there is something wrong in your script.

Creating a ‘Person’ Object

This expression initializes a new person object using a method from the output object. This can be any object you need.

var person = output.createPerson()

Setting property values

This expression assigns the email value from the input to the person's email property. Do this for all the properties you want to transform.

person.email.setValue (input.email)

Clear undefined properties

This example checks if the mobile phone number is undefined. If it is, it clears the existing value; otherwise, it sets the new value.

TypeScript
    if (input.mobilePhoneNumber.getValue() === undefined) {
        person.mobilePhoneNumber.clearValue();
    } else {
        person.mobilePhoneNumber.setValue(input.mobilePhoneNumber);
    }


Setting object property values

In this example, we are setting the employeeSince property of the employeeInformation object associated with the person.

The employeeInformation object can contain multiple properties related to the employee's details, such as their start date, position, and other relevant information.

TypeScript
    person.employeeInformation.employeeSince.setValue(input.employeeInformation.employeeSince)


Overwriting array property values

This example first clears any existing items in the array of products and then adds each product from the input array to the person's array.

TypeScript
    // Clear the existing items first
    person.arrayOfProducts.clearItems();

    // Now set the new values
    for (const products of input.arrayOfProducts) {person.arrayOfProducts.appendItem().setValue(products);}    


Appending array property values

The code appends items to an existing array (person.arrayOfProducts) by iterating over another array (input.arrayOfProducts). This means it adds new items to the end of the existing list without overwriting or replacing the current items in the array.

for (const products of input.arrayOfProducts) {person.arrayOfProducts.appendItem().setValue(products);}   

Creates a relation between objects

This expression establishes a relationship between the person and the specified company.

var relation = person.worksAt(company)

Sets a relation input value to a relation property

This example sets the 'employeeSince' property for the relationship using the input value.

TypeScript
    relation.employeeSince.setValue(input.employeeInformation.employeeSince)


Set default value when property value is empty (string only)

Sometimes, input data might not include certain property values. You can use default values, but only for type string as other types will not pass the empty validation.

// Set a default value if input.firstname is missing
person.firstname.setValue(input.firstname ?? "Unknown");


Deduplication of array values

If you're working with arrays, you can add more advanced logic, such as clearing the list and then deduplicate the new list if you only want to add unique values.

    // Clear existing items in the array
    person.arrayOfProducts.clearItems();

    // Create an array to track unique products
    const uniqueProducts = [];

    // Iterate through the input array
    for (const product of input.arrayOfProducts) {
        // Check if the product is already in the uniqueProducts array
        const productValue = product.getValue();
        if (uniqueProducts.indexOf(productValue) === -1) {
            // Add the product to the array (to track it as unique)
            uniqueProducts.push(productValue);

            // Append the product to the person's array
            person.arrayOfProducts.appendItem().setValue(product);
        }
    }