upsertData()

Overview

public struct function upsertData(
      required string  objectName             
    , required struct  data                   
    , required array   matchColumns           
    ,          array   updateColumns          
    ,          boolean insertManyToManyRecords = false
    ,          boolean isDraft                 = false
    ,          boolean useVersioning           = automatic
    ,          numeric versionNumber           = 0
    ,          boolean clearCaches             = Defaults to whether query caching is enabled or not for this object
    ,          numeric timeout                
)

Inserts a record or updates an existing record when a matching unique key already exists. Uses a single atomic database upsert statement where the adapter supports it, avoiding read-then-write races that can occur with separate update and insert calls.

Arguments

NameTypeRequiredDescription
objectNamestringYesName of the object whose record you want to upsert
datastructYesStructure of data to insert or update. Keys should map to properties on the object. All match columns must be present.
matchColumnsarrayYesArray of property names whose combined values identify an existing record via a unique index
updateColumnsarrayNoArray of property names to update when a matching record already exists. Defaults to all data properties that are not match columns.
insertManyToManyRecordsbooleanNo (default=false)Whether or not to insert multiple relationship records for properties that have a many-to-many relationship
isDraftbooleanNo (default=false)Whether or not the record change is a draft change
useVersioningbooleanNo (default=automatic)Whether or not to use the versioning system with the upsert
versionNumbernumericNo (default=0)If using versioning, specify a version number to save against
clearCachesbooleanNo (default=Defaults to whether query caching is enabled or not for this object)Whether or not to clear caches related to the object
timeoutnumericNoTimeout, in seconds, of the main upsert DB query

Examples

result = presideObjectService.upsertData(
objectName   = "system_config"
, data         = { category="myapp", setting="mysetting", value="myvalue", site="", tenant_id="" }
, matchColumns = [ "category", "setting", "site", "tenant_id" ]
);
// result.id       = record id
// result.inserted = true when a new record was created, false when an existing record was updated