How to call a BAM-generated function from an app

From your application, you can call a function generated in a BAM application.

To call a BAM-generated function in your non-BAM app, you must import the BAM module that contains the function. You must also define variables for the function's parameters, prior to calling the function in your app.

This topic uses a demonstration to illustrate the steps you must take. For this demonstration, you can use the OfficeStore sample project included in the Genero Studio distribution.

You will:
  • Create a simple app.
  • Save it to a module in a new application node.
  • Add code to import the module from which you want to call the function, and define variables. See the code in the example. For example, you will code to:
    • Import the module for the Order form file entity, OrderForm_ui.4gl.
    • Define variables for the Open Form function.
    • Call the function to open the form.
  • Run the application to test your app.
  1. Open the Genero Officestore project.
    From the Genero Studio Welcome Page, select the Tutorials and Samples tab. Select OfficeStore from the links displayed under Samples and Demos.
  2. Add a new Application node.
    1. Right-click on the Officestore Model node and select New Application.
    2. Name the node MyApp.
      You can use any name for this node; for this example we use the name MyApp.
  3. Add a new module to the application node.
    1. Right-click on the MyApp node and select New file > Genero > Source (.4gl).
    2. Save the file as MyApp.4gl as a child of the MyApp node.
      For the purpose of this demonstration, it does not matter where you save this new file on disk.
      An empty document opens in Code Editor.
    3. Create in this document a MAIN clause:
      MAIN
      END MAIN
    4. Save your changes.
  4. Find the function you want to call.
    Functions are found in the .4gl files in the Intermediate Files node under the Entities node. You can call any function for this purpose; for this example we use the OrderForm_ui_uiOpenForm function in the OrderForm_ui.4gl.
  5. Add code to your MyApp module to import the OrderForm module and save your changes.
    IMPORT FGL OrderForm_ui    
    
    MAIN
    END MAIN
  6. Import the BAM libraries, libdbappUI and libdbappFormUI.
    These libraries contain types for UI settings and constants you need to reference:
    IMPORT FGL OrderForm_ui    
    IMPORT FGL libdbappUI                   -- Import BAM libraries 
    IMPORT FGL libdbappFormUI
    
    MAIN
    END MAIN
  7. Define variables for the function parameters:
    In the example:
    1. l_uiSettings references the UISettings_Type that defines the UI Settings in the relation to the form in the BA diagram.
    2. l_whereRelation defines a string for the where clause in the query defined in the relation.
    3. errNo and actionNo define variables for the return function parameters.
    #...
    
    DEFINE l_uiSettings UISettings_Type
    DEFINE l_whereRelation STRING
    DEFINE errNo INTEGER
    DEFINE actionNo SMALLINT
    
    #...
  8. Add code to initialize the form's UI settings:
    In the example, the openMode and defaultMode values is set to C_MODE_UNDEFINED. This constant is defined in the libdbappFormUI library.
    #...
        LET l_uiSettings.openMode = C_MODE_UNDEFINED
        LET l_uiSettings.defaultMode = C_MODE_UNDEFINED
        LET l_uiSettings.disableDisplay = FALSE
        LET l_uiSettings.disableAdd = FALSE
        LET l_uiSettings.disableModify = FALSE
        LET l_uiSettings.disableDelete = FALSE
        LET l_uiSettings.disableSearch = FALSE
        LET l_uiSettings.disableEmpty = FALSE
    
        CALL l_uiSettings.transitions.clear()
    
        INITIALIZE l_whereRelation TO NULL
    #... 
  9. Add code to connect to the officestore database, close the default screen, and load action and style defaults from the "dbapp" templates.
     CONNECT TO "officestore"
     CLOSE WINDOW SCREEN
    
     CALL ui.Interface.loadActionDefaults("dbapp")
     CALL ui.Interface.loadStyles("dbapp")
  10. Add code to call the function to open the Order form.
    CALL OrderForm_ui_uiOpenForm(l_whereRelation, l_uiSettings.*) RETURNING errNo, actionNo
  11. Review the code to make sure you have the complete code shown in the example. Save your changes when done.
  12. Set a dependency between the MyApp application node and the Entities library node.
    This dependency is necessary because the MyApp application needs to find the OrderForm_ui.4gl, which is an external module in another library, to resolve the function.
    1. Right-click on the MyApp node and select Advanced Properties.
    2. Select the Dependencies page.
    3. Select the Entities node and click to activate the dependency.
    4. Click OK to apply the changes and close the dialog.
  13. Save the changes to your project.
  14. Build and execute the MyApp application.
    Right-click MyApp and select Execute.
    The application launches and the Order form opens.

Complete code sample

In this example, you have the code you need to open the Order form generated by the BAM. Copy and paste the code to your module.

IMPORT FGL OrderForm_ui -- Import module that has the function

IMPORT FGL libdbappUI -- Import BAM libraries referenced
IMPORT FGL libdbappFormUI

MAIN

    DEFINE l_uiSettings UISettings_Type
    DEFINE l_whereRelation STRING
    DEFINE errNo INTEGER
    DEFINE actionNo SMALLINT

    CONNECT TO "officestore"
    CLOSE WINDOW SCREEN
    CALL ui.Interface.loadActionDefaults("dbapp")
    CALL ui.Interface.loadStyles("dbapp")

    LET l_uiSettings.openMode = C_MODE_UNDEFINED
    LET l_uiSettings.defaultMode = C_MODE_UNDEFINED
    LET l_uiSettings.disableDisplay = FALSE
    LET l_uiSettings.disableAdd = FALSE
    LET l_uiSettings.disableModify = FALSE
    LET l_uiSettings.disableDelete = FALSE
    LET l_uiSettings.disableSearch = FALSE
    LET l_uiSettings.disableEmpty = FALSE

    CALL l_uiSettings.transitions.clear()

    INITIALIZE l_whereRelation TO NULL

    CALL OrderForm_ui_uiOpenForm(l_whereRelation, l_uiSettings.*) RETURNING errNo, actionNo

END MAIN