<object>.create
The <object>.create API creates a new record in the local database.
- If any column is defined as auto-generated, values are not allowed in them unless the trackChanges key is set to false.
- The record must contain all the mandatory columns and their respective values.
- The column names and values provided as key value pairs are case sensitive.
Quantum Visualizer (JavaScript)
Signature
<KNYObj>.create(record, options, successCallback, failureCallback)
Parameters
Parameter | Type | Description | Required |
record | JSON | Specify the record to be created in the database under a given object. | Yes |
options | JSON |
Option parameter accepts a HashMap that contains the following options keys:
For more information, refer Options Keys. |
Yes |
successCallback | Function | The function is invoked when records are created successfully. Newly created record is retuned through the success callback. | Yes |
failureCallback | Function | The function is invoked on an error with the cause of failure as an argument. | Yes |
Options
Parameter | Type | Description | Required |
trackChanges | Boolean |
If the trackChanges key is set to False, the record level operations are not tracked. When the option is set to False, the CUD operations performed on a record are not synced (uploaded). By default, the key is set to True. NOTE:
|
No |
markForUpload | JSON |
If markForUpload is set to False, the record changes are not uploaded to the server. By default, the option is set to True. NOTE:
|
No |
Return Type
void
Example
var category = new kony.sdk.KNYObj("CATEGORY"); var record = {}; // CATEGORY_ID is the autogenerated primary key record["CATEGORY_PN"] = Number("7"); record["CATEGORY_DES"] = "Creating a new record"; function successCallback(response) { kony.print("Record created successfully with primary keys: " + response["CATEGORY_ID"]); } function errorCallback(error) { kony.print("Create is failed" + JSON.stringify(error)); } category.create(record, {}, successCallback, errorCallback); //Disable Change Tracking var options = { "trackChanges": false }; var KNYObject = new kony.sdk.KNYObj("CATEGORY"); KNYObject.create(record, options, successCallback, failureCallback); //Mark for Upload var options = { "markForUpload": false }; category.create(record, options, successCallback, errorCallback);
Android (Java)
Signature
void <KNYObj>.create(final HashMap<String, Object>record, final HashMap<String, Object> options, KNYCallback callback) throws Exception
Parameters
Parameter | Type | Description | Required |
record | HashMap<String, Object> | Specify the record to be created in the database under a given object. | Yes |
options | HashMap<String, Object> | For future use, supply null or empty HashMap as options. | Yes |
callback |
KNYCallback | Application must implement onSuccess and onFailure methods of KNYCallback interface. Newly created record is retuned through onSuccess callback and onFailure handler is invoked with cause of failure as an argument in case of error. | Yes |
Sync Options
Parameter | Type | Description | Required |
trackChanges | Boolean |
Set the trackChanges key to false, the record level operations are not tracked. Hence, the records are not synced (uploaded). NOTE: You must provide values for primary key columns when trackChanges is set to false. |
No |
markForUpload | HashMap | Set the markForUpload to false, the record changes are not uploaded to the server. | No |
Return Type
void
Example
try { //Create object in which the record should be created private KNYObj syncObject = new KNYObj("CATEGORY"); //CATEGORY_ID is autogenerated primary key HashMap < String, Object > newRecord = new HashMap < > (); newRecord.put("CATEGORY_PN", "2"); newRecord.put("CATEGORY_DES", "Vegetables"); //HashMap for options. HashMap < String, Object > ; options = new HashMap < > (); syncObject.create(newRecord, options, new KNYCallback() { @Override public void onSuccess(Object response) { Log.d("Primary key of the newly created record:" + response.get("CATEGORY_ID")); } @Override public void onFailure(Object error) { OfflineObjectsException e = (OfflineObjectsException) error; Log.e("Object creation failed with error:" + e.getMessage()); } }); } catch (Exception e) { Log.e("Object creation failed with error:" + e.getMessage()); } //Disable change tracking Hashmap < String, Object > options = new HashMap < > (); options.put(KSPublicConstants.TRACK_CHANGES, false); sdkObjectSync.create(record, options, new KNYCallback() { @Override public void onSuccess(Object response) {} @Override public void onFailure(Object error) {} }); //Mark for Upload HashMap < String, Object > ; options = new HashMap < String, Object > (); options.put("markForUpload", false); syncObject.create(newRecord, options, new KNYCallback() { @Override public void onSuccess(Object response) {} @Override public void onFailure(Object error) {} });
iOS (Objective C)
Signature
void <KNYObj> create:(NSDictionary <NSString *, id> *) record options:(NSDictionary <NSString *, id> *)options onSuccess:(KNYSuccessCompletionHandler)onSuccess onFailure:(KNYFailureCompletionHandler)onFailure
Parameters
Parameter | Type | Description | Required |
record | NSDictionary<NSString*, id> | Specify the record to be created in the database under a given object. | Yes |
options | NSDictionary<NSString*, id> | For future use, supply null or empty NSDictionary as options. | Yes |
onSuccess | KNYSuccessCompletionHandler | The function is invoked when records are created successfully. Newly created record is retuned through the success callback. | Yes |
onFailure | KNYFailureCompletionHandler | The function is invoked on an error with the cause of failure as an argument. | Yes |
Sync Options
Parameter | Type | Description | Required |
trackChanges | Boolean |
Set the trackChanges key to false, the record level operations are not tracked. Hence, the records are not synced (uploaded). NOTE: You must provide values for primary key columns when trackChanges is set to false. |
No |
markForUpload | NSDictionary<NSString*, id> | Set the markForUpload to false, the record changes are not uploaded to the server. | No |
Return Type
void
Examples
KNYObj * _categoryObject = [ [KNYObj alloc] initWithName: @"CATEGORY" error: & error ]; NSMutableDictionary * newRecord = [NSMutableDictionary new] newRecord[@"CATEGORY_DES"] = @"New record"; newRecord[@"CATEGORY_PN"] = @"7"; [_categoryObject create: newRecord options: @{} onSuccess: ^ (id response) { NSLog(@"Create record is successful, primary key: %@", [response objectForKey: @"CATEGORY_ID"]); } onFailure: ^ (id object) { OfflineObjectsError * error = (OfflineObjectsError) object; NSLog(@"Create record Failed with error %@", [error.userInfo localizedDescription]); } ]; //Disable change tracking NSDictionary < NSString * , id > * options = @ { KNYCONSTANTS_TRACK_CHANGES: @NO }; [sdkObjectSync create: record options: options onSuccess: ^ (id object) {} onFailure: ^ (id object) {} ]; //Mark for Upload NSDictionary < NSString * , id > * options = @ { KNYCONSTANTS_MARK_FOR_UPLOAD: @NO }; [sdkObjectSync create: record options: options onSuccess: ^ (id object) {} onFailure: ^ (id object) {} ];
- The trackChanges flag must be used consistently (either always true or always false) for all the CUD operations on a record. You must not update the value of change tracking flag in between the CUD operations on a record.
- When you set both markForUpload and trackChanges flags, an error is reported as these are mutually exclusive options.
- The change tracking option for Cascade Delete is also applicable for the child records.
- For Hierarchical Objects, you must provide a proper value for both parent and child record operations.