# JSCalculation

## Overview

The `JSCalculation` object is a solution model wrapper designed to define and manage custom calculations associated with data sources. It allows for full customization of calculation logic, variable types, and storage options, enabling developers to seamlessly integrate business logic into their data models.

Through its flexible API, the object provides access to essential details like calculation code and metadata while offering tools to evaluate storage status and configuration. This ensures calculations are adaptable to varying application requirements, maintaining both clarity and efficiency in solution development.

## Properties Summarized

| Type                                                                         | Name                          | Summary                                                                       |
| ---------------------------------------------------------------------------- | ----------------------------- | ----------------------------------------------------------------------------- |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) | [code](#code)                 | The full source code of this method (including doc and function declaration). |
| [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) | [variableType](#variabletype) | Get or set the sql type of this variable.                                     |

## Methods Summarized

| Type                                                                           | Name                           | Summary                                                  |
| ------------------------------------------------------------------------------ | ------------------------------ | -------------------------------------------------------- |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)   | [getName()](#getname)          | This method returns the name of the stored calculation.  |
| [UUID](https://docs.servoy.com/reference/servoycore/dev-api/application/uuid)  | [getUUID()](#getuuid)          | Returns the UUID of the calculation.                     |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) | [hasFlag(flag)](#hasflag-flag) | Check a flag of the calculation.                         |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) | [isStored()](#isstored)        | Returns whether this calculation is a stored one or not. |

## Properties Detailed

### code

The full source code of this method (including doc and function declaration).

**Type**\
[String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The declaration code of the calculation.

**Sample**

```js
var calc = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myCalculation");
calc.code = "function myCalculation() { return 123; }";
```

### variableType

Get or set the sql type of this variable.\
Type should be one of JSVariable.DATETIME, JSVariable.TEXT, JSVariable.NUMBER , JSVariable.INTEGER or JSVariable.MEDIA.

**Type**\
[Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) The SQL type of this variable.

**Sample**

```js
var calc = solutionModel.getDataSourceNode("db:/example_data/customers").getCalculation("myCalculation");
calc.variableType = JSVariable.DATETIME;
```

## Methods Detailed

### getName()

This method returns the name of the stored calculation.

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) the name of the stored calculation

**Sample**

```js
var calc = solutionModel.newCalculation("function myCalculation() { return 123; }", JSVariable.INTEGER, "db:/example_data/customers");
application.output(calc.getName());
```

### getUUID()

Returns the UUID of the calculation.

**Returns:** [UUID](https://docs.servoy.com/reference/servoycore/dev-api/application/uuid) The UUID of the calculation.

**Sample**

```js
var calc = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation() { return 123; }", JSVariable.INTEGER);
application.output(calc.getUUID().toString());
```

### hasFlag(flag)

Check a flag of the calculation.\
The flags are a bit pattern consisting of 1 or more of the following bits:\
&#x20;\- JSColumn.UUID\_COLUMN

**Parameters**

* [Number](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/number) **flag** ;

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) boolean whether flag is set.

**Sample**

```js
var datasourceNode = solutionModel.getDataSourceNode('db:/example_data/orders')
var calculation = datasourceNode.getCalculation('mycalculation')
if (calculation.hasFlag(JSColumn.UUID_COLUMN))
{
	 // calculation was typed as UUID
}
```

### isStored()

Returns whether this calculation is a stored one or not.

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) true if the calculation is stored, false otherwise

**Sample**

```js
var calc = solutionModel.getDataSourceNode("db:/example_data/customers").newCalculation("function myCalculation() { return 123; }", JSVariable.INTEGER);
if (calc.isStored()) application.output("The calculation is stored");
else application.output("The calculation is not stored");
```

***
