Components | All | New | MacOS | Windows | Linux | iOS | ||||
Examples | Mac & Win | Server | Client | Guides | Statistic | FMM | Blog | Deprecated | Old |
JSON.InsertOrUpdateRecord
Inserts or updates a record in a table in one line using JSON.
Component | Version | macOS | Windows | Linux | Server | iOS SDK |
JSON | 14.4 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
MBS( "JSON.InsertOrUpdateRecord"; FileName; TableName; PrimaryKeys; Values ) More
Parameters
Parameter | Description | Example |
---|---|---|
FileName | The file name of where the table is inside. Can be empty to look for the table in all files. | Get(FileName) |
TableName | The name of the table to insert record into. Can be ID of table, so we lookup name by ID. Can be result of GetFieldName() function as we remove field name automatically. |
"Assets" |
PrimaryKeys | The JSON with keys and values which provides an unique ID for each record, so we can identify the field. Can be RecordID for the record ID. Field name can be the result of GetFieldName() function as we remove table name automatically. If field name is a number with field ID, so we lookup name by ID. |
"ID" |
Values | The JSON object with the record. |
Result
Returns OK or error.
Description
Inserts or updates a record in a table in one line using JSON.If the insert fails due to duplicate ID, we will update the record values.
You pass to this function table name (and optional filename).
To identify the record you pass the name of the ID field and the value for the record. The ID field must be unique.
Then you pass a JSON with pairs of field names and values for each field you like to change in the record.
This function combines JSON.InsertRecord and JSON.UpdateRecord. If the ID is already used, we update, else we insert a new record.
You can specify fields and table via IDs and the plugin will lookup them for you at runtime. Table IDs and table names must be unique across all open files for this to work correctly.
You can use GetFieldName() function to query field names and the plugin removes the table prefix with :: in the field name before passing it to SQL engine.
You can use FM.ExecuteSQL.LastSQL function to see later what SQL was created and run.
The SQL functions in FileMaker do not trigger OnWindowsTransaction script trigger.
If the record is locked, you get an error like this: [MBS] ERROR: (301): Record is locked by another user.
Your script may then wait and try again later.
Does not take part in transactions in FileMaker. Changes are made directly to the database, so you can't revert them later with Revert Transaction script step.
We auto convert text values to date, time or timestamp if the plugin sees the fields having such a type. Otherwise the type of JSON value defines the data type. If JSON contains an array or object, we make text from it.
You can use FM.SQL.Execute function to query records in FileMaker and get them as JSON with FM.SQL.JSONRecords function. If needed, use JSON.ReplaceEvaluate or JSON.Replace to adjust the JSON and make modifications. Then use JSON.InsertRecord or JSON.InsertOrUpdateRecord to insert them.
Examples
Insert or update record:
MBS("JSON.InsertOrUpdateRecord"; ""; "Contacts";
JSONSetElement ( "{}" ;
GetFieldName ( Contacts::ID ) ; "123" ; JSONString );
JSONSetElement (
JSONSetElement ( "{}" ;
GetFieldName ( Contacts::FirstName ); "Joe"; JSONString);
GetFieldName ( Contacts::TheNumber ); 125; JSONNumber)
)
See also
- FM.ExecuteSQL.LastSQL
- FM.InsertOrUpdateRecord2
- FM.InsertOrUpdateRecord3
- FM.SQL.Execute
- FM.SQL.JSONRecord
- FM.SQL.JSONRecords
- JSON.InsertRecord
- JSON.ReplaceEvaluate
- SQL.Execute
- SQL.InsertOrUpdateRecords
Release notes
- Version 14.4
- Added JSON.InsertRecord and JSON.InsertOrUpdateRecord functions.
Example Databases
Blog Entries
- New in MBS FileMaker Plugin 14.4
- Neues MBS Plugin 14.4 für Claris FileMaker
- MBS Plugin 14.4 for Claris FileMaker
- Insert or update with JSON
- MBS FileMaker Plugin, version 14.4pr4
This function checks for a license.
Created 21st August 2024, last changed 4th September 2024