This is an old revision of the document!


Agents

Agents run periodically and execute code specified by the user.

Note: Agents are only available in environments that run Protogrid 2.1 or higher.

Setup

To set up an agent, there are two necessary steps:

  • Under Design → Code, create an Agent Library and add your code to the on_schedule function.
  • Under Design → Proto, select the Proto you want to use and add corresponding the Agent Library

Example

  1. Create a Proto with a DateTime Field and a Text Field. Copy the key of the Text Field.
  2. Create a user profile with rights to edit the proto created above.
  3. Create an Agent Library, specify the user created above as context user and copy the following code. Replace the value of defkey_status_field with the key from above.
  4. Add this Agent Library to your Proto.
  5. Based on this proto, create some cards where the date lies within the next 60 minutes. Wait one hour.
  6. The value in your Text Field should have been replaced by Frist abgelaufen.
function agent_library() {
    var defkey_status_field = "3b47a5fe-894d-4580-bc0c-563aa83d9a02";
 
    function set_value(card, field_key, value) {
        //sets the value of a single-value field given by field_key
        for (var i = 0; i < card.design_elements.length; i++) {
            if (card.design_elements[i]['definition_key'] === field_key) {
                card.design_elements[i]['value'] = value;
                break;
            }
        }
        return card;
    }
 
    function on_schedule(card, cookie) {
        card = set_value(card, defkey_status_field, 'Frist abgelaufen');
        return card;
    }
 
    return {
        set_value: set_value,
        on_schedule: on_schedule
    };
}

Execution

Agents are checked every hour. If any DateTime Field on a card has a smaller value than the current time and the Agent has not yet run since that time, the Agent gets triggered and the code in the Agent Library is executed in the context of this card (for Date-Only Fields, the time taken into account is said date, 00:00 GMT). Agents run in the context of a specific user profile. This user's password is automatically reset during the execution process. So this user profile shall only be used for agents and not for physical users. All agent executions are logged in the agent_log on the respective card.

Best Practices for Calculation Code

In the following, we have compiled some recommendations based on our experience for high stability, performance, reliability and maintainability of backend code that performs calculations on a larger number of Cards (i.e. more than 100):

  1. It is recommended to use views of dedicated Search Dialog Boxes for retrieving the needed source data for calculation code. Do not use multi column indexing views for backend code.
    1. Note: Use multi column indexind views at all only if the free combination of several filters is required in the user interface.
  2. Write to as few Relation Fields as possible in the code. Do this only if it is needed for the user interface (e.g. for filtering TableViews). Do this only on Cards that are rarely/never edited. Do this only on Cards that do not (already) have a large number of Relating Cards.
    1. Procedure if the calculation code requires a “relation” that does not need a Relation Field in the user interface: Use a Text Field instead of a Relation Field. Write the key of the relation to this Text Field with an individual prefix (important!). Use a Text Filter or a dedicated Search Dialog Box to retrieve Cards with the desired relation.
  3. If editing or deleting a Card in the user interface subsequently to calculation has to be prevented, this can be done in both cases by restricting the editing rights (roles at Proto and/or Card level).
  4. Read/load as few specific/single cards as possible. Work with views whenever possible. Use “include_cards” - but only if necessary. Do not hesitate to contact Protogrid Customer Support proactively in case of special requirements (e.g. certain values needed in the value of a view row or special reduction views needed).
  5. Analogous to the previous point, write as few specific/single cards as possible. Whenever possible, save several cards together. You can use the Endpoint /api/v2/apps/<app_name>/cards with a list of Card objects in the POST body.
Print/export