Skip to main content
Notably, the most complex mass operation method, Upsert, performs any combination of inserts, updates, and, in some cases, deletes. It requires the same parameters as the Insert method, but adds a string array of keys, and an UpsertNomatchAction enum.

Key fields

A key is a column name, and the keys parameter is an array of keys that contain either:
  • The primary key column name.
  • One or more table column names that match none or one unique database record.
When the target table is a built-in table, and the keys parameter contains just the primary key column name, the operation iterates all records in the dataset and determines whether it should perform an UPDATE and an INSERT for that row. Records in the dataset with a matching primary key are updated while records with a primary key value of 0 are inserted. When the target table in an extra table and the keys parameter contains just the primary key column name, the only permissible action is an Update. The reason is that the id field must also be included in the columns array to match a table record. If an extra-table id field in the incoming data matrix is null or "0", an exception is thrown with the message extra tables use database-assigned primary keys, therefore the primary key column cannot be specified for Insert. When the key parameter contains one or more column names, the Upsert operation finds the first database row where there is a matching dataset key record data and performs an Update. Matches must be unique.

Matching behavior

The UpsertNomatchAction enum option is used to determine what action to take on records that do not match the key criteria. When using the Agent API, the only option available is the boolean parameter deleteUnmatched. When true, any rows that do not match a record are deleted. In this case, when the method is finished, the table mirrors the incoming data. If that is the intent, it might be faster to consider using a Truncate and then an Insert instead. When targeting user-defined tables (udxxxsmall or udxxxlarge), the UpserNomatchAction must be set to NoChange. When using the Agent API, deleteUnmatched must be set to false.

User-defined fields

When updating user-defined fields, DeleteUnmatched must be false.
When targeting user-defined fields tables, such as udcontsmall or udcontlarge, the column names can be program ID (progid) instead of the table column name, such as long05. Upsert is always oriented towards one table. Therefore when the target table is a user-defined table, only specify ProgIDs for one of the user-defined tables at a time. Target either udfXxxSmall or udfXxxLarge, ensuring not to mix columns from both tables in one Upsert. When updating User-defined field, specify the entity primary key column as the key, and primary key value in the data array. Any new UDEF rows are automatically linked to their owner entity.
A failed Upsert can leave the database in a partially updated state. There is extensive validation, but there are no guarantees. Make sure to inspect the MassResults return type and review what changes have occurred.

Example table upsert example

Rental Gear table:
This will update 3 rows in the y_rental_gear table. It will match the records by code with ID 3 and 4, and update their quantity and description fields.

Contact table upsert example

See contact table in database reference guide.

User-defined field upsert example

See uddocsmsall table in the database reference guide.
CS
Upsert can be asked to return a row-by-row status: for each row you get back your key columns, and an enum specifying what happened (insert/update/delete/zero/nochange).