Skip to content

Ensure

Adds or updates rows in the DataTable and DataTableDictionary based on the specified key and properties.

1
myDataTableDictionary.Ensure(key: string, input: object):( DataRow | Array<DataRow> )

Updates or creates a row with the given input properties. If the key does not exist, initializes a new row with the initInput properties.

1
myDataTableDictionary.Ensure(key: string, input: object, initInput: object):( DataRow | Array<DataRow> )

Parameters

string key
    The key of the row(s) to be updated or added. Required.

object input
    The row properties to be updated.

object initInput
    Row properties to be updated if no match is found.

Returns

The matching DataRow instance or instances. The matching DataRow instance or instances.

Remarks

The initProperties argument is used to initialize a new row if no matching row is found in the DataTable.
This method inserts or updates rows in the DataTable.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
var table = $Database.Get({
    Parameters : {
        TargetSchema : 'HR',
        TargetTable : 'Groups'
    }
});

var list = table.ToDictionary('Name');

var myGroup = list.Ensure("MyGroup", {
    Name : 'MyGroup',
    Code : '123'
}, {
    Id : Script.NewId()
});

list.Save();

The initProperties argument is used to initialize a new row if no matching row is found in the DataTable.
This method inserts or updates rows in the DataTable.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
var table = $Database.Get({
    Parameters : {
        TargetSchema : 'HR',
        TargetTable : 'Groups'
    }
});

var list = table.ToDictionary('Name');

var myGroup = list.Ensure("MyGroup", {
    Name : 'MyGroup',
    Code : '123'
}, {
    Id : Script.NewId()
});

list.Save();

See Also