Logic: DeviceApply

As soon as we press the "Apply" button we want to write the changes to the database. If we changed an existing record we want to perform an update and if created a new record we want to add it. The logic to do so is as follows:

IF F-deviceNumber.Device = 0 
	ERROR "Please enter a Device Number."
	FOCUS entryDeviceNumber
	RETURN
ENDIF

IF V-isPresentDevice = TRUE()
	LET O-Device = R-Device
	DATAMODEL UPDATE Device
ELSE
	LET O-Device = R-Device
	DATAMODEL APPEND Device
ENDIF
IF DATAMODELSTATUS() <> 0 
	ERROR "Error updating Device."
	RETURN
ENDIF
MESSAGE "Device has been updated."
LET V-isPresentDevice = TRUE()

After we check if a valid number was entered, we check if we either update an exising record or if we add a new one. To update the database using the logic command DATAMODEL, we first have to make sure that the current record in the DataModel contains the correct values. We know that the correct values are in the table buffer "Device", since we have linked data references like F-deviceNumber.Parts to the screen. To get these values to the DataModel "Device", you can either assign them one by one (M-deviceNumber.Device = F-deviceNumber.Device), or by assigning a complete table buffer record to the DataModel using the Group Data Reference prefixes O- (DataModel) and R- (table buffer Record).

After an DATAMODEL UPDATE or APPEND, we have to check if the update went OK, by testing the a return status made available through the built-in function DATAMODELSTATUS

Finally we inform the user by updating a message on the message bar and we have to set the Variable isPresentDevice to TRUE since even if we added a record, it is present now.

Save the logic as "DeviceApply" and test your application. You should now be able to add new and modify existing records in the database.