# switch script node

<div align="left"><img src="https://111806075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mg4otljWU9FsHKYhTsW%2Fuploads%2F0dalv02EN2Y67eUalRfo%2Fimage.png?alt=media&#x26;token=ad11803a-04d7-4ab2-9c5e-5d670de84a50" alt=""></div>

Routes incoming Message to one OR multiple output chains. Node executes configured JavaScript function.

JavaScript function receive 3 input parameters:

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

The script should return ***an array of next Relation names*** where Message should be routed. If returned array is empty - message will not be routed to any Node and discarded.

![](https://111806075-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mg4otljWU9FsHKYhTsW%2Fuploads%2FniGuGX2Riu5SPKKJ2b80%2Fimage.png?alt=media\&token=26953633-3b1a-4331-86de-dc55d4956128)

Message payload can be accessed via `msg` variable. For example `msg.temperature < 10;`\
Message metadata can be accessed via `metadata` variable. For example `metadata.customerName === 'John';`\
Message type can be accessed via `msgType` variable. For example `msgType === 'POST_TELEMETRY_REQUEST'`<br>

Full script example:

```javascript
if (msgType === 'POST_TELEMETRY_REQUEST') {
    if (msg.temperature < 18) {
        return ['Low Temperature Telemetry'];
    } else {
        return ['Normal Temperature Telemetry'];
    }
} else if (msgType === 'POST_ATTRIBUTES_REQUEST') {
    if (msg.currentState === 'IDLE') {
        return ['Idle State', 'Update State Attribute'];
    } else if (msg.currentState === 'RUNNING') {
        return ['Running State', 'Update State Attribute'];
    } else {
        return ['Unknown State'];
    }
}
return [];
```
