How to call an external module

From your BAM-generated application, you can call a function in an external module.

To call an external function, you must add custom code using a code event. With this custom code, you will import the module that defines the function, prior to calling the function.

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.

In this demonstration, you will:
  • Create a simple function that displays "foo".
  • Save the function to a module in a new library node.
  • Call the function from the On Open Form code event of a form.
  • Run the application to test the execution of the function.

Complete these steps:

  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 Library node.
    1. Right-click on the Officestore Model node and select New Library.
    2. Name the library myExtLib.
      You can use any name for this node; for this example we use the name myExtLib.
  3. Add a new function to the library node.
    1. Right-click on the myExtLib node and select New file > Genero > Source (.4gl).
    2. Save the file as myExtFunc.4gl file as a child of the myExtLib 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 simple function that displays "foo":
      PUBLIC FUNCTION foo()
          DISPLAY "foo"
      END FUNCTION
    4. Save your changes.
  4. Set a dependency between the myExtLib node and the Orders library node.

    This dependency is necessary because the Orders application needs to find myExtFunc.4gl, which is an external module in another library, to resolve the function.

    1. Right-click on the Orders node and select Advanced Properties.
    2. Select the Dependencies page.
    3. Select the myExtLib node and click to activate the dependency.
    4. Click OK to apply the changes and close the dialog.
  5. Expand the Entities node and open the OrderForm.4fdm file.
    1. Select the root node (ManagedForm) in the Form Structure view.
    2. Select On Open Form under Dialog Events in the Properties view.
      You can call the function from any code event; for this example we are using the On Open Form code event.
    3. Select the arrow in the Value field.
      This opens a source file where the code of the required function is generated for you.
  6. Update this file by adding your user code.
    Ignore any errors or warnings you see prior to the building and execution of the application.
    1. Add the import statement for your module containing the function.
      IMPORT FGL myExtFunc
      Add this towards the top of the file, after the existing IMPORT FGL statements.
    2. Add a call to your function in the dlgEvent_OnOpenForm() function.
      PUBLIC FUNCTION dlgEvent_OnOpenForm(currentForm ui.form)
          DISPLAY "dlgEvent_OnOpenForm (Form scope) is raised"
          CALL myExtFunc.foo()
      END FUNCTION
  7. Save the changes to your project.
  8. Build and execute the Orders application.
    Right-click Orders and select Execute.
    The application launches and the OrderForm opens. "Foo" is displayed in the Output view.