Event Listener and Trigger

Prerequisites

Procedure

The purpose of this guide is to show you how the neptune.events.Trigger and neptune.events.Listener work together. In this guide we will create two Events where, the event listerner will run custom functions be triggered once the trigger event initiates. In this simple set up, we will have an event

Step 1. Set up first Event

  1. Open Events and click add

  2. Give it a name. In this example, the name is ListenerExample

  3. Set Event Action to App

This Event will work as a "listener" and will trigger custom actions that we will set.

event listener

Step 2. Set up script for the second Event

  1. Open a Project and add a new script

  2. Give it a name. In this example, the name is app.js

  3. Add the logic

The script will accept the payload and update a given table. Once that is done, we can pass data to the ListenerExample Event and process it anyway we desire.

try {

    const eventResponse = {
        process: payload.process,
        data: null,
    }

    switch (payload.process) {
        case "updateTable":
            await entities.inspections_terry.createQueryBuilder()
                .update()
                .set(payload.data.change)
                .where("id = :id", { id: payload.data.id })
                .execute();
            eventResponse.data = {"status":"Update happended!"}
            break;
        default:
            log.error("Process not handled:", payload.process);
            break;
    }

    await p9.events.publish("ListenerExample", eventResponse) // this function initiates the event listener
    complete();

} catch (err) {
    log.error(err)
    complete();
}
In this example, we have dragged and dropped a Table in the script

Step 3. Create the second Event

  1. Open Events and click add

  2. Give it a name. In this example, the name is TriggerExample

  3. Set Event Action to Script

  4. Select the script you have just created

event trigger

Setting up the neptune.events components

For the neptune.events.Trigger:

  1. In the App Designer, drag and drop a neptune.events.Trigger in Resources

  2. At event choose the trigger event. In this case, it is TriggerExample

For the neptune.events.Listener:

  1. Drag and drop a neptune.events.Listener in Resources

  2. At event choose the listener event. In this case, it is ListenerExample

Regarding how we handle the response we pass back to the ListenerExample, we will define a function that will be triggered each time ListenerExample is called.

In a script past the below:

function handleData(payload)  {
    switch (payload.process) {
        case "updateTable":
            const data = payload.data
            console.log("Response data from script: ",data)
            break;
        default:
            console.log("Not handled");
            break;
    }
}

Now, we need to refer this function in the actions of the neptune.events.Listener.

event listener action

Execution

There are many ways to trigger this process. In this example, a button once pressed will trigger the TriggerExample Event and pass data.

In the Press Event add the following:

var payload = {
    process:"updateTable",
    data: {
        id:"EDC4F5BA-3DE0-4BE7-9B50-010D667CC684", // id of an entry located in the table
        change: {
            "status" : "Pending"   // altering "status"
        }
    }
}
triggerEventTrigger(payload);

Results

After clicking the button, the table will update and the function we defined will be triggered.

Button that triggers the progress

event button

Response from the handleData() function

event logs

Table before clicking

event table before

Table after clicking

event table after