Lesson 4 the Events

Create an Event listener

After we created the library InventoryService we can now use it in the workflows to manage the inventory entity. But before using it in a workflow we first need to create a listener that watches on purchase_invoice entity for update, insert and delete.

To create an Event listener click on Develop app, and then click on the Event listener menu.

Here we have to click on the Add button located above the list. In the page that opens we need to fill in:

  • Entity = purchase_invoice (should be selected from the list that appears while typing)

After that we need to check:

  • Before INSERT
  • Before UPDATE
  • Before INSERT/UPDATE
  • Before DELETE

Click the Save button.

Using InventoryService

Now that we created the Event listener for the purchase_invoice entity, we can use the InventoryService that we created earlier. The creation of the Event listener generated some workflows. The one that interests us is one that it’s called PurchaseInvoiceOperations.

To edit this workflow go to Develop app, and then click on Workflows menu. In the list that appear find the workflow named PurchaseInvoiceOperations, select that row and click the Edit button located above the list (next to Add button).

Here you will find the following methods:

  • onBI – that will be executed before inserting a purchase_invoice into the database
  • onBU – that will be executed before updating a purchase_invoice
  • onBIU – that will be executed before inserting and updating a purchase_invoice
  • onBD – that will be executed before deleting a purchase_invoice entity.

In order to use the InventoryService we first need to import it in the workflow. To do that add the following line after the name of the workflow:

import InventoryService alias is;

Now we can refer to this service using its alias.

The code for this four methods should be like this:

The method onBIU will be used to calculate the amount for each invoice item, by multiplying the unit_price with the quantity.

method atomic onBIU(e as ENTITY purchase_invoice) {
    foreach ii in CHILD e purchase_invoice_purchase_invoice_item {
        ii.amount = ii.unit_price * ii.quantity;
    }
}

When we create a new purchase_invoice we need also to assign it a document entity. In order to do that, before inserting the new entity to the database, we have have to create a document entity. To do that we will use the function createDocument of the library InventoryService.

For each purchase_invoice_item of the purchase_invoice we need to create an inventory entity. To do that we will use the method createIn that we created earlier on InventoryService.

method atomic onBI(e as ENTITY purchase_invoice) {
    e.id_document = is->createDocument();

    foreach ii in CHILD e purchase_invoice_purchase_invoice_item {
        is->createIn(e.id_document, ii.id_product, ii.quantity, "new purchase invoice");
    }
}

The method onBU will first delete all inventory records based on the id_document of the invoice and then will create an inventory entity for each invoice item.

method atomic onBU(e as ENTITY purchase_invoice) {
    is->delete(e.id_document);

    foreach ii in CHILD e purchase_invoice_purchase_invoice_item {
        is->createIn(e.id_document, ii.id_product, ii.quantity, "add item in purchase invoice");
    }
}

The method onBD will delete all inventory records based on the id_document of the invoice, before deleting the invoice.

method atomic onBD(e as ENTITY purchase_invoice) {
    is->delete(e.id_document);
}

After you entered the code above for each method in the PurchaseInvoiceOperations workflow, click on the Save button.

Update the search view of the product

Now that we set up how the inventory entity is created or deleted, we need a way to see the inventory for each product. In order to do that, we need to modify the FETCH of the product search view created in Lesson 1 the Products.

Go to the Develop app, and then click on Search view menu.

In page that opens locate the search view with the title “Products” and name “productSearch, check it in and click the Edit button located above the list.

Select the Fetch tab, and enter the following fetch query:

FETCH product(key GROUP BY, name, price, bar_code) {
    inventory TO id_product LINK TYPE LEFT (SUM(quantity))
}
FILTER AND (${nameArg})

Click on the Save button.

Now you should be able to keep track of your product inventory.