The following sample code illustrates the use of the OERA ServiceAdapter to update data using a BusinessEntity from the client in custom code (outside the SmartBusinessEntityAdapter). Similar actions can be taken using the ServiceInterface when executing code already on the Backend.

Before you can update existing data you need to load the data you plan to modify using the ServiceAdapter. This is similar to the requirement to FIND a record in the database before you can update it. The following wiki article describes how you can use the ServiceAdapter to read data from a Business Entity http://jira.consultingwerkcloud.com:8090/display/SCL/Using+the+ServiceAdapter+to+access+a+Business+Entity+to+retrieve+data+from+the+client.

When creating new records that should be submitted to the Business Entity for storing them in the database we do not need to retrieve data from the Backend first.


After loading the data from the OrderBusinessEntity (populating the dataset dsOrder) we are ready to update some values.


Consultingwerk.Util.DatasetHelper:SetTrackingChanges (DATASET dsOrder:HANDLE, TRUE) .
   
FOR EACH eOrder:     
    DISPLAY eOrder.OrderNum  eOrder.SalesRep eOrder.OrderDate.     
    UPDATE eOrder.OrderDate eOrder.OrderStatus .  
END.
 
Consultingwerk.Util.DatasetHelper:SetTrackingChanges (DATASET dsOrder:HANDLE, FALSE) .

DEFINE VARIABLE hChanges AS HANDLE NO-UNDO. DEFINE VARIABLE cContext AS CHARACTER NO-UNDO. DEFINE VARIABLE cMessages AS CHARACTER NO-UNDO.

Before doing any modification (create, update, delete records) on the data contained in the Dataset, you must activate the TRACKING-CHANGES property on all tables of the Dataset. This can easily be achieved using the SetTrackingChanges method of the Consultingwerk.Util.DatasetHelper. After we are finished with the changes we need to set the TRACKING-CHANGES to false for all tables of the Dataset.


The GetChangesDataset method of the Consultingwerk.Util.DatasetHelper returns a new Dataset instance populated with only the changed records of the client Dataset. This reduced set of data is less expensive on the network and makes the execution of the following SubmitData method faster.

hChanges = Consultingwerk.Util.DatasetHelper:GetChangesDataset (DATASET dsOrder:HANDLE) .


You can submit the changes to the Backend using the SubmitData method of the Consultingwerk.Framework.FrameworkSettings:ServiceAdapter.

Consultingwerk.Framework.FrameworkSettings:ServiceAdapter:SubmitData ("",
                                                                      "Consultingwerk.SmartComponentsDemo.OERA.Sports2000.OrderBusinessEntity",
                                                                      INPUT-OUTPUT DATASET-HANDLE hChanges,
                                                                      INPUT-OUTPUT cContext) .

hChanges = Consultingwerk.Util.DatasetHelper:GetChangesDataset (DATASET dsOrder:HANDLE) .

  

Now after submitting the data to the Backend we need to check for possible errors during the processing of the data. The Business Entity or Data Access class validation routines have stored possible validation messages in the ERROR-STRING property of the associated record buffers. The DatasetErrorStrings method of the ErrorHelper class is the simplest way to return all error messages contained in the Dataset.

 

A message starting with „errors:“ followed by all returned errors will be shown if there are errors returned after the call to SubmitData. The dataset is able to store errors per record so we might have multiple errors.

In case there were no errors after submitting the changes, the new values will be merged and accepted for the current dataset. This will end the roundtrip of the updated data and the change information will the wiped out.


cMessages = Consultingwerk.Util.ErrorHelper:DatasetErrorStrings (hChanges) .
 
IF cMessages > "" THEN
    MESSAGE "errors:" cMessages
        VIEW-AS ALERT-BOX.
ELSE DO:
    hChanges:MERGE-CHANGES (DATASET dsOrder:HANDLE) .
    DATASET dsOrder:ACCEPT-CHANGES () .
    DELETE OBJECT hChanges .
END.