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.
  • 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 virtual folder under the src library node.
    1. Right-click on the src library node and select New Virtual Folder.
    2. Name the virtual folder myExtLib.
      You can use any name for this node; for this example we use the name myExtLib.
  3. Create the new function.
    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. Locate the code event where you will call your new function.
    1. Open the OrderForm.4fdm file.
      It is located in the Entities virtual folder, or can be accessed from the BA diagram.
    2. Select the root node (ManagedForm) in the Form Structure view.
    3. 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.
    4. Select the arrow in the Value field.
      This opens a source file where the code of the required function is generated for you.
  5. 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
  6. Save the changes to your project.
  7. Build and execute the Orders application.
    Right-click Orders_prg and select Execute.
    The application launches and the OrderForm opens. "foo" is displayed in the Output view.