Save Data Record
Save Data Record
The
DataSource.saveRecord()
method can be used to create a new record or to update an existing record.
The data record to be saved is typically sent to the event-handler method from Java Script code. The event-handler method can declare and use it as follows:
public void submitWorkRequest(DataRecord workRequest) {
DataSource ds = ...
ds.saveRecord(workRequest);
}
To update an existing record, the
DataRecord
object must contain not only the updated field values, but their old values as well. This allows the DataSource to find and update the correct record even if its primary keys have been changed by the user. The old values are managed automatically when the record is saved from a standard UI control, or when your custom code use the
DataRecord
API to set new values:
public void approveWorkRequest(DataRecord workRequest) {
DataSource ds = ...
// set status to Approved
workRequest.setValue('wr.status', 'Approved'); // preserves a copy of old value
// update work request
ds.saveRecord(workRequest);
}
If the
DataRecord.isNew
property is set to true, and the main table has an auto-generated primary key, the
saveRecord
call returns a record containing the generated primary key value:
public String createWorkOrder(String type, Date dateAssigned) {
DataSource ds = ...
DataRecord workOrder = ds.createNewRecord();
workOrder.setValue("wo.wo_type", type);
workOrder.setValue("wo.date_assigned", dateAssigned);
DataRecord primaryKeyRecord = ds.saveRecord(workOrder);
return primaryKeyRecord.getValue("wr.wr_id");
}
If you need to set a field value to NULL, pass an empty string to the
DataRecord.setValue()
method:
workOrder.setValue("wo.date_assigned", "");
You can also pass NULL values to the
DataRecord.setNullableValue()
method:
workOrder.setNullableValue("wo.date_assigned", NULL);