# Add Employee Field Transformation

A transformation automatically updates an employee field’s value based on a JavaScript function you define. This is useful for deriving values, formatting data, or calculating results from other employee information.

***

### How Transformations Work

* A transformation runs every time any employee information changes.
* It receives two arguments:
  * employee – The current employee object with all field values.
  * history – Historical values for the employee’s fields.
* You can use [Lodash](https://lodash.com/) functions directly, e.g. sortBy(...).
* You can also use [date-fns](https://date-fns.org/) for date operations.
* The function should return:
  * null to clear the field value.
  * The new value to set for the field.

***

### Add a Transformation

1. Go to Data → Employee Fields in the left-hand menu.
2. Find the field you want to transform in the list.
3. Click the three dots menu at the end of the row.
4. Select Add transformation.
5. In the editor, write your JavaScript function using the provided employee and history arguments.
6. (Optional) Click Test to run your code and see the output.
7. Click Save to activate the transformation.

***

### Example: Calculate Years of Service

```javascript
function transform(employee) {
  const startDate = employee["startdate"];
  if (!startDate) return null;
  const years = differenceInYears(new Date(), new Date(startDate))
  return years?.toString() ?? null;
}
```

This transformation:

* Reads the employee’s startdate field.
* Uses differenceInYears from date-fns to calculate how many full years have passed.
* Stores the result as a string in the field.

***

### Tips

* Keep transformations small and focused for easier debugging.
* Always handle cases where source data may be missing.
* Test before saving to ensure the logic behaves as expected.
* Remember: transformations affect this specific field only.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.introist.com/knowledge-base/build/data/employee-fields/add-employee-field-transformation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
