# Persisting Controller State

There are a number of cases where you will need to save the state of a controller:

* You want to run the controller in an external browser but want the settings in the browser instance to be the same as the one in the Captivate UI
* You want to have the settings loaded whenever you open the same project
* You want to have the settings loaded whenever you add this controller to any project

The following methods are available for that:

#### `saveSettings` method <a href="#savesettings-method" id="savesettings-method"></a>

Store an input-specific settings string with the project.

```javascript
// callback method
ServiceHandler.scheduler.saveSettings(inputName, settingsString, callback);

// promises method
await ServiceHandler.scheduler.saveSettings(inputName, settingsString);
```

* `inputName`: Name of the input that should hold the settings (can be anything for short-term settings, but must match the name of an attached input for it to be saved with the project).
* `settingsString`: String value to store.
* `callback`: This function will always be called with a single `null` argument

**Example:**

```javascript
let settings = {
  version: 1,
  showColors: true,
  myLabel: 'Hello, World',
};

// note the use of JSON.stringify here
ServiceHandler.scheduler.saveSettings('Simple HTML Input', JSON.stringify(settings));
```

#### `loadSettings` method <a href="#loadsettings-method" id="loadsettings-method"></a>

Load an input-specific settings string that was saved with the project.

```javascript
// callback method
ServiceHandler.scheduler.loadSettings(inputName, callback);

// promises method
await ServiceHandler.scheduler.loadSettings(inputName);
```

* `inputName`: Input behavior name matching the defintion XML.
* `callback`: Function with a single argument that will receive the settings *string* once it is retrieved.

**Example:**

```javascript
ServiceHandler.scheduler.loadSettings('Simple HTML Input', (settingsString) => {
  try {
    let settings = JSON.parse(settingsString);
    // process settings
  } catch (e) {
    // handle JSON parsing errors here.
  }
});
```

#### `saveGlobal` method <a href="#saveglobal-method" id="saveglobal-method"></a>

Save a string of data to the Captivate system so it can be used by any controller in any project.

```javascript
// callback method
ServiceHandler.scheduler.saveGlobal(settingsKey, settingsString, callback);

// promises method
await ServiceHandler.scheduler.saveGlobal(settingsKey, settingsString);
```

* `settingsKey`: The key used to identify this settings string in the global settings database.
* `settingsString`: String value to store.

**Example:**

```javascript
const globalSettingsName = 'My Controller: Globals';
const settings = {
  version: 1,
  showColors: true,
  myLabel: 'Hello, World',
};

// note the use of JSON.stringify here
ServiceHandler.scheduler.saveGlobal(globalSettingsName, JSON.stringify(settings));
```

#### `loadGlobal` method <a href="#loadglobal-method" id="loadglobal-method"></a>

Load a settings string that was saved to the global settings with a specific key.

```javascript
// callback method
ServiceHandler.scheduler.loadGlobal(settingsKey, callback);

// promises method
await ServiceHandler.scheduler.loadGlobal(settingsKey);
```

* `key`: The key used to identify this settings string in the global settings database.

**Example:**

```javascript
const globalSettingsName = 'My Controller: Globals';
const settingsString = await ServiceHandler.scheduler.loadSettings(globalSettingsName);
try {
  let settings = JSON.parse(settingsString);
  // process settings
} catch (e) {
  // handle JSON parsing errors here.
}
```


---

# 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://developers.newbluefx.com/captivate-api/javascript-api-reference/using-the-servicehandler/persisting-controller-state.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.
