# script transformation

<div align="left"><img src="https://111806075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mg4otljWU9FsHKYhTsW%2Fuploads%2FU1YVQ8Cz9spOaHYsD5JZ%2Fimage.png?alt=media&#x26;token=346d1afc-94f1-44e1-8f67-2c7f9340fe96" alt=""></div>

Changes Message payload, Metadata or Message type using [c](https://docs.tesenso.com/tesenso-iot-cloud/rule-engine/rule-engine/test-javascript-function)[onfigured JavaScript function](https://docs.tesenso.com/tesenso-iot-cloud/rule-engine/rule-engine/test-javascript-function).

JavaScript function receives 3 input parameters:

* `msg` - is a Message payload.
* `metadata` - is a Message metadata.
* `msgType` - is a Message type.

Script should return the following structure:

```javascript
{   
    msg: new payload,
    metadata: new metadata,
    msgType: new msgType 
}
```

<div align="left"><img src="https://111806075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mg4otljWU9FsHKYhTsW%2Fuploads%2FGMav1eTCdm7Y0EXG3jJH%2Fimage.png?alt=media&#x26;token=93eac143-3b36-40c4-a05b-33a405810a76" alt=""></div>

All fields in resulting object are optional and will be taken from original message if not specified.

Outbound Message from this Node will be new Message that was constructed using configured JavaScript function.

JavaScript transform function can be verified using [Test JavaScript function.](https://docs.tesenso.com/tesenso-iot-cloud/rule-engine/rule-engine/test-javascript-function)

\
**Example**

Node receives Message with **payload**:

```javascript
{
    "temperature": 22.4,
    "humidity": 78
}
```

Original **Metadata**:

```java
{ "sensorType" : "temperature" }
```

Original **Message Type** - POST\_TELEMETRY\_REQUEST<br>

The following modifications should be performed:

* change message type to ‘CUSTOM\_UPDATE’
* add additional attribute ***version*** into payload with value ***v1.1***
* change ***sensorType*** attribute value in Metadata to ***roomTemp***

The following transform function will perform all necessary modifications:

```javascript
var newType = "CUSTOM_UPDATE";
msg.version = "v1.1";
metadata.sensorType = "roomTemp"
return {msg: msg, metadata: metadata, msgType: newType};
```
