Skip to content

Ensure

Adds or updates rows in DataTable and DataTableDictionary by specified key and properties .

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

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

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

Parameters

string key
    Required. Key of rows to be updated or added.

object input
    Row properties to be updated.

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

Returns

Matching row instance. Matching row instance.

Remarks

initProperties argument used as to initialize a new row no matching row found in DataTable
This method inserts or updates rows in 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();

initProperties argument used as to initialize a new row no matching row found in DataTable
This method inserts or updates rows in 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