# JSRecord

## Overview

The `JSRecord` class represents a single row within a `JSFoundset`, extending the capabilities of `JSBaseSQLRecord`. It provides various methods and properties to access, modify, validate, and manage records in Servoy.

## Functionality

This class offers properties to retrieve the parent foundset, check for validation errors through `recordMarkers`, and access any exceptions encountered during operations. It allows developers to track and manage the state of a record, such as checking whether it is new, edited, or deleted.

The `JSRecord` methods include features to revert unsaved changes, save records, and retrieve data about the record, such as its primary keys, data source, and outstanding changed data. Developers can also create custom validation markers for additional record checks and validations. The class ensures flexibility by providing utilities for checking the loaded state of related foundsets and handling record edits efficiently.

For more details, refer to the [JSFoundset](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsfoundset) documentation.

## **Extends**

[JSBaseSQLRecord](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsbasesqlrecord)

## Properties Summarized

| Type                                                                                                     | Name                            | Summary                                                                                                                                                                                     |
| -------------------------------------------------------------------------------------------------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Exception](https://github.com/Servoy/gitbook/blob/master/reference/servoycore/dev-api/exception.md)     | [exception](#exception)         | Returns last occurred exception on this record (or null).                                                                                                                                   |
| [JSFoundSet](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsfoundset)           | [foundset](#foundset)           | Returns parent foundset of the record.                                                                                                                                                      |
| [JSRecordMarkers](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsrecordmarkers) | [recordMarkers](#recordmarkers) | Returns the validation object if there where validation failures for this record Can be set to null again if you checked the problems, will also be set to null when a save was successful. |

## Methods Summarized

| Type                                                                                                     | Name                                                                           | Summary                                                                                                                       |
| -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------- |
| [JSRecordMarkers](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsrecordmarkers) | [createMarkers()](#createmarkers)                                              | Creates and returns a new validation object for this record, which allows for markers to be used outside the validation flow. |
| [JSDataSet](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsdataset)             | [getChangedData()](#getchangeddata)                                            | Returns a JSDataSet with outstanding (not saved) changed data of this record.                                                 |
| [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string)                             | [getDataSource()](#getdatasource)                                              | Returns the records datasource string.                                                                                        |
| [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array)                               | [getPKs()](#getpks)                                                            | Returns an array with the primary key values of the record.                                                                   |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                           | [hasChangedData()](#haschangeddata)                                            | Returns true if the current record has outstanding/changed data.                                                              |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                           | [isDeleted()](#isdeleted)                                                      | Returns true if the current record is a deleted record or false otherwise.                                                    |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                           | [isEditing()](#isediting)                                                      | Returns true or false if the record is being edited or not.                                                                   |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                           | [isNew()](#isnew)                                                              | Returns true if the current record is a new record or false otherwise.                                                        |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                           | [isRelatedFoundSetLoaded(relationName)](#isrelatedfoundsetloaded-relationname) | Returns true or false if the related foundset is already loaded.                                                              |
| void                                                                                                     | [revertChanges()](#revertchanges)                                              | Reverts the in memory outstanding (not saved) changes of the record.                                                          |
| [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean)                           | [save()](#save)                                                                | Saves the in memory outstanding (not saved) changes of the record and stops its editing.                                      |

## Properties Detailed

### exception

Returns last occurred exception on this record (or null).

**Type**\
[Exception](https://github.com/Servoy/gitbook/blob/master/reference/servoycore/dev-api/exception.md) The occurred exception.

**Sample**

```js
var exception = record.exception;
```

### foundset

Returns parent foundset of the record.

**Type**\
[JSFoundSet](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsfoundset) The parent foundset of the record.

**Sample**

```js
var parent = record.foundset;
```

### recordMarkers

Returns the validation object if there where validation failures for this record Can be set to null again if you checked the problems, will also be set to null when a save was successful.

**Type**\
[JSRecordMarkers](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsrecordmarkers) The last validation object if the record was not validated.

**Sample**

```js
var recordMarkers = record.recordMarkers;
```

## Methods Detailed

### createMarkers()

Creates and returns a new validation object for this record, which allows for markers to be used outside the validation flow.\
Will overwrite the current markers if present.\
Can be set to null again if you checked the problems, will also be set to null when a save was successful.

**Returns:** [JSRecordMarkers](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsrecordmarkers) A new validation object.

**Sample**

```js
var recordMarkers = record.createMarkers();
```

### getChangedData()

Returns a JSDataSet with outstanding (not saved) changed data of this record.\
column1 is the column name, colum2 is the old data and column3 is the new data.

NOTE: To return an array of records with outstanding changed data, see the function databaseManager.getEditedRecords().

**Returns:** [JSDataSet](https://docs.servoy.com/reference/servoycore/dev-api/database-manager/jsdataset) a JSDataSet with the changed data of this record.

**Sample**

```js
/** @type {JSDataSet} */
var dataset = record.getChangedData()
for( var i = 1 ; i <= dataset.getMaxRowIndex() ; i++ )
{
	application.output(dataset.getValue(i,1) +' '+ dataset.getValue(i,2) +' '+ dataset.getValue(i,3));
}
```

### getDataSource()

Returns the records datasource string.

**Returns:** [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) The datasource string of this record.

**Sample**

```js
var ds = record.getDataSource();
```

### getPKs()

Returns an array with the primary key values of the record.

**Returns:** [Array](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/array) an Array with the pk values.

**Sample**

```js
var pks = foundset.getSelectedRecord().getPKs() // also foundset.getRecord can be used
```

### hasChangedData()

Returns true if the current record has outstanding/changed data.

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) true if the current record has outstanding/changed data.

**Sample**

```js
var hasChanged = record.hasChangedData();
```

### isDeleted()

Returns true if the current record is a deleted record or false otherwise.\
The deletion has not been done in the database itself.

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

**Sample**

```js
var isNew = foundset.getSelectedRecord().isDeleted();
```

### isEditing()

Returns true or false if the record is being edited or not.

This will not check if the record doesn't really have any changes, it just returns the edit state.\
So this can return true but databaseManager.getEditedRecord() will not return this record because that\
call will check if the record has really any changed values compared to the stored database values.\
Record can be in edit mode without changes when some field is focused (so edit is started) but no changes are done yet\
or when changes were done in such a way that record data is the same as database data.

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) a boolean when in edit.

**Sample**

```js
var isEditing = foundset.getSelectedRecord().isEditing() // also foundset.getRecord can be used
```

### isNew()

Returns true if the current record is a new record or false otherwise. New record means not saved to database.

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

**Sample**

```js
var isNew = foundset.getSelectedRecord().isNew();
```

### isRelatedFoundSetLoaded(relationName)

Returns true or false if the related foundset is already loaded. Will not load the related foundset.

**Parameters**

* [String](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/string) **relationName** name of the relation to check for

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) a boolean when loaded.

**Sample**

```js
var isLoaded = foundset.getSelectedRecord().isRelatedFoundSetLoaded(relationName)
```

### revertChanges()

Reverts the in memory outstanding (not saved) changes of the record.

**Returns:** void

**Sample**

```js
var record= foundset.getSelectedRecord();
record.revertChanges();
```

### save()

Saves the in memory outstanding (not saved) changes of the record and stops its editing.

**Returns:** [Boolean](https://docs.servoy.com/reference/servoycore/dev-api/js-lib/boolean) true if the save was done without an error.

**Sample**

```js
var record= foundset.getSelectedRecord();
record.save();
```

***
