How do I send notifications depending on different conditions via Foundry Actions?

89 Views Asked by At

I have a workshop module where I want a notification to go to a different set of users depending on whether a certain property on the object in question has been changed by an action or not. In other words, I want to conditionally route my notification when I submit my action.

1

There are 1 best solutions below

0
On BEST ANSWER

In essence, you’ll have to create an action that takes an input to determine whether the property has been changed, then send this input through to a function backed notification, and return a different user group depending on the value of the input you just passed through to the action.

Firstly, you’ll need to configure your input for the action. Since we want to determine whether a property on an object has been changed by an action or not, we will capture the value of that property before the action has been submitted. The screenshot like below will show you how to setup a variable in workshop to do this. enter image description here Then, set up your action so that there is an additional parameter for this variable/input. Pass through this input into workshop like the screenshot below. enter image description here After that, you’ll want to write your function to return the correct user group depending on the condition. Here’s an example below. Notice how there are two values, prevValue and newValue. prevValue is equivalent to the variable you just set up via workshop to capture the value of the object property before action submission, and newValue is the parameter for that same object property being changed on the action form, so map them accordingly.

import { Function, Integer, Notification, OntologyEditFunction, ShortNotification, EmailNotificationContent, User, Users, Group } from "@foundry/functions-api";

// Uncomment the import statement below to start importing object types
import { Objects } from "@foundry/ontology-api";

export class MyFunctions {

   @Function()
    public async conditionalReturnUser(prevValue: Integer, newValue: Integer) : Promise<User[] | Group>{
        let comparison = JSON.stringify(prevValue) === JSON.stringify(newValue);
        if (comparison){  
            let group = await Users.getGroupByIdAsync('27c733a4-0feb-48a7-9613-143e9ea61a84');
            return group!;
        }
        else {
            let user = await Users.getUserByIdAsync('5eb712e8-6b4b-442b-9689-5b0470dfde40');
            let group = await Users.getGroupByIdAsync('27c733a4-0feb-48a7-9613-143e9ea61a84');
            return [user!];
        }
    }
    
}

In the action configuration screen, set up your notification like so: enter image description here