1 Context:
In this example, we are writing our own EventHandler that assigns a specific value to a datafield in the backend through a routine. To do this, we create the EventHandler and connect it to a DSO to predefine the values and display a message in a datafield.
2 Step by step guide:
- 👋 For this task we will use the screen we used for the Frontend button again. Please use your copy of “TrainingDay4_OrderOverview”
- 👋 In your ProCode Workspace create a new folder in “/src/backend/ -> and name it “MyEventHandler”
- 👋 Create a new file -> “EventHandler<YourName>.cls”
- 👋 Start typing “b1-datasourceEventhandler” and select the template, then save your code by pressing “CTRL+ S”
- 👋 Navigate back to your Low-Code Workspace and go to “Integrate” -> “Data” -> “Datasource” and select the button “Generate Dataset-Definiton” in the toolbar
- 👋 Search for the “Data Source Object” -> “OrderDSO” and click on “Generate Definition”
- 👋 Copy the “Dataset Definition”
- 👋 Navigate back to your Pro-Code Workspace and create a new file in your newly created folder “MyEventHandler” and call it “dsOrder.i” and paste your copied “Dataset Definition” in there, then press “Strg + S”
- 👋 Include the dsOrder.i in the class:
CLASS <YourFolder>.<YourEventhandler>
INHERITS Akioma.Swat.OERA.Dynamic.BaseServerEventHandler:
{ <YourFolder>/dsOrder.i &ACCESS="PRIVATE" }
- 👋 Rename the constructor’s to: “EventHandler<yourName>”
- 👋 Insert the term “dsData” in the dataset
CONSTRUCTOR <YourEventhandler>():
SUPER(DATASET dsData :HANDLE).
THIS-OBJECT:TriggerOnBeforeSave = TRUE.
THIS-OBJECT:TriggerOnAfterFetch = TRUE.
END CONSTRUCTOR.
- 👋 Write the following code for your method:
METHOD PROTECTED OVERRIDE VOID OnAfterFetch():
FOR EACH eOrder NO-LOCK:
ASSIGN eOrder.Instructions = "You made it :) This is your first backend logic. Awesome".
END.
END METHOD.
- 👋 Now press “Strg + S” and execute a “b1 trim” in the terminal -> This will restart the “PASOE” without loosing your current session
- 👋 Now go back to your Low-Code Workspace and navigate to “Integrate” -> “Data” -> “Datasource”
- 👋 Search for the “OrderDSO” and open the designer for it
- 👋 Search for the attribute “ServerEventHandler” in the “Attributes” panel and fill in the value -> “MyEventHandler.EventHandler<YourName>”, then select “Save”
- 👋 Navigate to “Design” -> “Screens” and launch your screen to see the result
Full Code Example:
BLOCK-LEVEL ON ERROR UNDO, THROW.
CLASS <YourFolder>.<YourEventhandler>
INHERITS Akioma.Swat.OERA.Dynamic.BaseServerEventHandler:
{ <YourFolder>/<yourDataset>.i &ACCESS="PRIVATE" }
CONSTRUCTOR <YourEventhandler>():
SUPER(DATASET dsData :HANDLE).
THIS-OBJECT:TriggerOnBeforeSave = TRUE.
THIS-OBJECT:TriggerOnAfterFetch = TRUE.
END CONSTRUCTOR.
METHOD PROTECTED OVERRIDE VOID OnBeforeSave():
// enter before save code here
END METHOD.
METHOD PROTECTED OVERRIDE VOID OnAfterFetch():
FOR EACH eOrder NO-LOCK:
ASSIGN eOrder.Instructions = "You made it :) This is your first backend call. Awesome".
END. END METHOD.
END CLASS.
3 Explanation:
Every time data is retrieved, our defined event handler is called and our defined logic is executed. This demonstrates how easy it is to integrate and use custom code in the backend. The message gets generated in the defined field with every fetch.