# StateUtil

`StateUtil` provides utility methods for managing and manipulating state properties in your application.

### Integer Operations

**`inc(statePropertyId: String): Integer`**

* Increment the value of an integer state property by 1.
* Parameters:
  * `statePropertyId` (type: String) - The identifier of the state property.
* Returns: An Integer representing the updated value of the state property.

```typescript
// Increment an integer state property
var incrementedValue = StateUtil.inc("counter");
```

***

**`dec(statePropertyId: String): Integer`**

* Decrement the value of an integer state property by 1.
* Parameters:&#x20;
  * `statePropertyId` (type: String) - The identifier of the state property.
* Returns: An Integer representing the updated value of the state property.

```typescript
// Decrement an integer state property
var decrementedValue = StateUtil.dec("counter");
```

***

**`plus(statePropertyId: String, delta: Integer): Integer`**

* Add a specified value (delta) to an integer state property.
* Parameters:
  * `statePropertyId` (type: String) - The identifier of the state property.
  * `delta` (type: Integer) - The value to add.
* Returns: An Integer representing the updated value of the state property.

```typescript
// Add a value to an integer state property
var delta = 10;
var newValue = StateUtil.plus("counter", delta);
```

***

**`counter(statePropertyId: String): Integer`**

* Retrieve the current value of an integer state property.
* Parameters:
  * `statePropertyId` (type: String) - The identifier of the state property.
* Returns:
  * An Integer representing the current value of the state property.

```typescript
// Get the current value of an integer state property
var currentValue = StateUtil.counter("counter");
```

***

### Integer and Long Property Access:

**`getInteger(statePropertyId: String): Integer`**

* Retrieve the value of an integer state property.
* Parameters:
  * `statePropertyId` (type: String) - The identifier of the state property.
* Returns:
  * An Integer representing the value of the state property.

```typescript
// get integer state proeprty
var age = StateUtil.getInteger("age");
```

***

**`setInteger(statePropertyId: String, value: Integer): void`**

* Set the value of an integer state property.
* Parameters:
  * `statePropertyId` (type: String) - The identifier of the state property.
  * `value` (type: Integer) - The new value to set.

```typescript
// Set integer state properties
StateUtil.setInteger("age", 30);
```

***

**`setLong(statePropertyId: String, value: Long): void`**

* Set the value of a long state property.
* Parameters:
  * `statePropertyId` (type: String) - The identifier of the state property.
  * `value` (type: Long) - The new value to set.

```typescript
// Set long state property
StateUtil.setLong("timestamp", 1635180000000);
```

***

**`getLong(statePropertyId: String): Long`**

* Retrieve the value of a long state property.
* Parameters:
  * `statePropertyId` (type: String) - The identifier of the state property.
* Returns:
  * A Long representing the value of the state property.

<pre class="language-javascript"><code class="lang-javascript"><strong>// get long state property
</strong><strong>var timestamp = StateUtil.getLong("timestamp");
</strong></code></pre>

***

### String Property Access:

**`getString(statePropertyId: String): String`**

* Retrieve the value of a string state property.
* Parameters:
  * `statePropertyId` (type: String) - The identifier of the state property.
* Returns:
  * A String representing the value of the state property.

***

**`setString(statePropertyId: String, value: String): void`**

* Set the value of a string state property.
* Parameters:
  * `statePropertyId` (type: String) - The identifier of the state property.
  * `value` (type: String) - The new value to set.

```typescript
// Get and set string state properties
StateUtil.setString("name", "Alice");
var name = StateUtil.getString("name");
```

***

### JSON Property Access:

**`setJson(statePropertyId: String, value: JsonNode): void`**

* Set the value of a JSON state property.
* Parameters:
  * `statePropertyId` (type: String) - The identifier of the state property.
  * `value` (type: JsonNode) - The new JSON value to set.

```typescript
// Get and set JSON state properties
var jsonData = JsonUtil.toJson({ "key": "value" });
StateUtil.setJson("data", jsonData);
```

***

**`getJson(statePropertyId: String): JsonNode`**

* Retrieve the value of a JSON state property.
* Parameters:
  * `statePropertyId` (type: String) - The identifier of the state property.
* Returns:
  * A `JsonNode` representing the JSON value of the state property.

```typescript
var jsonValue = StateUtil.getJson("data"); // returns key: "value"
```

***

### Examples

**Example: Managing Integer State Property (e.g., Count):**

```javascript
// Initial state: Counter is set to 5
StateUtil.setInteger("counter", 5);

// Increment the counter by 1
var incrementedValue = StateUtil.inc("counter");
// Result: The counter is now 6

// Decrement the counter by 1
var decrementedValue = StateUtil.dec("counter");
// Result: The counter is back to 5

// Add 10 to the counter
var delta = 10;
var newValue = StateUtil.plus("counter", delta);
// Result: The counter is now 15

// Retrieve the current counter value
var currentValue = StateUtil.counter("counter");
// Result: The current value is 15
```

**Example: Managing String State Property (e.g., User Name):**

```javascript
// Set the user's name
StateUtil.setString("userName", "Alice");

// Retrieve the user's name
var name = StateUtil.getString("userName");
// Result: The user's name is "Alice"
```

**Example: Managing JSON State Property (e.g., User Data):**

```javascript
// Define a JSON object
var userData = JsonUtil.toJson({ "name": "Bob", "age": 28 });

// Set the user data in the state
StateUtil.setJson("userData", userData);

// Retrieve the user data from the state
var jsonValue = StateUtil.getJson("userData");
// Result: jsonValue contains the JSON data with name "Bob" and age 28
```

These examples demonstrate how the `StateUtil` methods can be used to manage and manipulate different types of state properties in your application, making it easy to work with integers, strings, and JSON data in a business context.


---

# 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.b2winsuite.com/5.4/scripting/utilities/stateutil.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.
