The following examples illustrate some of the ways you can define tasks that can be triggered by app events. See the ClickToSendEmailsample app to see some of these examples.
Send email when a row is added, updated, or deleted
You can create an event that sends a email when a row is added, updated, or deleted. For example, you might send an email each time an Order
is added or updated.
The ServiceRequests
table and the On Add in ServiceRequests_Action1
task illustrate how to send an email each time a new Service Request
is created. For example:
-
The
ServiceRequests
table contains the customer's name in columnName
, the customer's email address in theEmail
column, and anEnum
value that indicates whether the service request isPending
,Active
, orCompleted
. -
The
On Add in ServiceRequests_Action1
task can be triggered on the AppSheet backend service each time a new row is added to theServiceRequests
table. It can then generate an email and send it to the email address contains in theEmail
column.
Send email when a row is updated to have a specific column value
You can create an event that sends an email when a row is updated to have a specific column value. For example, you might send an email when the Status
column in the ServiceRequests
row is updated to contain the value Active
.
AND(
("Active" = [_THISROW_AFTER].[Status]),
([_THISROW_AFTER].[Status] <> [_THISROW_BEFORE].[Status])
)
In this expression, note the use of [_THISROW_BEFORE]
and [_THISROW_AFTER]
. These are system-provided references that refer to the state of the row before and after the change was made, respectively. This is really useful in order to check if the value of a field changed from one value to another.
Because [_THISROW_BEFORE]
and [_THISROW_AFTER]
are references, you use a dereference expression to access the value of a specific column in the referenced row.
Send email when a row is updated to have two or more specific column values
The ServiceRequests
table and the On Change in ServiceRequests_Action1
task illustrate how to send an email when a new service request is in Active
state and all needed parts are in hand. For example:
-
The
ServiceRequests
table contains the customer's name in columnName
, the customer's email address in columnEmail
, anEnum
column that indicates whether the service request isPending
,Active
, orCompleted
, and aYes/No
column that indicates whether parts are on order. -
The
On Change in ServiceRequests_Action1
task can be triggered on the AppSheet backend service each time a row in theServiceRequests
table is updated. For example, the following expression sends email to the email address in theEmail
column when either theStatus
orPartsOnOrder
columns are changed, theStatus
isActive
, and there are noPartsOnOrder
. By checking whetherStatus
orPartsOnOrder
have just changed, we avoid sending email when some other column in the row has changed that is irrelevant to whether the service request is being worked on.
AND(
("Active" = [_THISROW_AFTER].[Status]),
NOT([_THISROW_AFTER].[PartsOnOrder]),
OR(
("Active" <> [_THISROW_BEFORE].[Status]),
[_THISROW_BEFORE].[PartsOnOrder]
)
)
Send email that includes file attachments
You can create an event that allows the user to control the file attachments appended to the email. For example, you might have a brochure for each product you offer. Each brochure might reside in its own PDF file. The customer could select which products they are interested in and you could attach the appropriate PDF file for each product to the email.
The Customers
table and the On Change in Customers_Action1
task illustrate how to send an email that includes one or more file attachments selected dynamically by the client. For example, the app user can select the images or PDF documents to include with the email by selecting values from an EnumList
. For example:
-
The
Customers
table contains the customer's name in columnName
, the customer's email address in columnEmail
, and anEnumList
of potential email attachments in columnAttachments
. -
When the user edits a row in the
Customers
table they can select one or more values from theAttachments
EnumList
. In our case, the user can selectApple
,Banana
, orOrange
, or any combination of the three. WhenApple
is selected, the fileApple.pdf
is included as an email attachment. Likewise forBanana
andOrange
. The user selected values are stored in theAttachments
field of theCustomers
row. -
The
On Change in Customers_Action1
task can be triggered on the AppSheet backend service each time theCustomers
table is updated. For example, the following expression determines if columnAttachments
has a value and if its value has changed. If so, it sends email to the email address contained in theEmail
column.
AND(
ISNOTBLANK([_THISROW_AFTER].[Attachments]),
([_THISROW_AFTER].[Attachments] <> [_THISROW_BEFORE].[Attachments])
)
The event can determine which PDF files to attach to the email using the following expression:
<<IFS(IN("Apple", [Attachments]), {"Apple.pdf"}) + IFS(IN("Banana", [Attachments]), {"Banana.pdf"}) + IFS(IN("Orange", [Attachments]), {"Orange.pdf"})>>
The Apple.pdf
, Banana.pdf
, and Orange.pdf
files must reside on the server in the application folder.
Send an email from an Action button
You can use an action button on the app to perform a data change. As a result, a single click of an action button in the app can trigger a customized email message that is sent to one or more recipients by the AppSheet backend service. You can label the action button to make it clear that an email will be sent when the action button is clicked.
The Orders
table and the On Change in Orders_Action1
task can be used to send email from an action button. For example:
-
The
Orders
table includes the email columnEmail
and the numeric columnEmailsSent
. -
When the user is viewing a row in the
Orders
table, they can click the Email This Order action button. That increments the value in columnEmailsSent
. -
The
On Change in Orders_Action1
task can be triggered on the AppSheet backend service each time theOrders
table is updated. For example, the following expression determines if columnEmailsSent
has changed. If so, it generates an email and sends it to the email address contains in columnEmail
.
AND(
ISNOTBLANK([_THISROW_AFTER].[EmailsSent]),
([_THISROW_AFTER].[EmailsSent] <> [_THISROW_BEFORE].[EmailsSent])
)
Choose and send a report from a list of reports
The Reports
table and the ReportOpenOrders_Action1
and ReportActiveServiceRequests_Action1
tasks can be used to choose and send a report from a list of reports. For example:
- The
Reports
table contains a list of reports each of which has a report name and description. - A user can go to the menu, select the
Reports
view, choose a report from the list of Reports, and click the Create action to trigger the selected report. - The Create action updates the value in the
LastRun
DateTime
field of the chosenReports
record. - The
Reports
record update triggers either theReportOpenOrders_Action1
andReportActiveServiceRequests_Action1
task depending on theName
contained in theReports
record. - The event generates a report and sends it to the appropriate email recipients.
Send an email only after adding a parent record and all of its children
When you add a parent record along with one or more child records, AppSheet first adds the parent record followed by each of the child records. This can make it difficult to trigger an automation only after all of the child records have been added.
You can can use the following technique to ensure that all parent and child records are added before the automation runs.
This technique uses a Form Saved
event to trigger a data-change action which triggers the task only after the child records have been added.
For example:
-
The child table must contain a
Ref
field to the parent table. Set the Is a part of property of theRef
field to On. This makes it possible to add child records when the parent record is added. -
Add a
Text
field to the parent table. Call the fieldStatus
and set its Initial value property to""
(blank). TheStatus
column controls whether the event fires. -
Create a Data Change action for the parent table which sets the
Status
column's value to"Run"
. -
Create a second Data Change action for the parent table which sets the
Status
column's value to""
(blank). -
Create a Composite Action for the parent table that invokes the two Data Change actions above. This Composite Action invokes the first Data Change action to set the
Status
column's value to"Run"
, and the second Data Change action to set theStatus
column's value to blank. These two Data Changes will be sent to the server as separate updates. The first update fires the event, while the second update prevents further updates from firing the event. -
Go to the UX > Views tab. Click Show system views. Select the form for the parent table. Expand Behavior. Find Event Actions and set its Form Saved property to the Data Composite action created in the previous step.
-
Set your event's Event Type property to Updates only. Set its Condition property to:
[Status] = "Run"
Maintain per user settings
You can maintain "per user" settings. For example, you might want to allow each user to specify whether they prefer to receive an email or an SMS when a workflow is triggered.
You can store the settings as follows:
-
Create a new table having one record per user. Each record will contain that user's settings. You might call the table
Automation Settings
. -
Create a slice on the
Automation Settings
table so that each user "sees" only their own settings row. You might call the sliceAutomation Settings Slice
. You cannot use a security filter because then one user's settings would not be visible to the other user's event. -
Create a view that allows each user to update the settings in their
Automation Settings Slice
.
You can use the preferences as follows:
-
In your event, check the preferences contained in the
Automation Settings Slice
for the current user to control what action is taken.
For example, if you wish to allow each user to specify whether they prefer to receive an email or an SMS when a workflow is triggered, do the following:
-
Define both an Email and an SMS event.
-
In the Email event Condition property, check whether the current user wishes to receive an email. In the SMS event Condition property, check whether the current user wishes to receive an SMS.
Invoke other actions
We have described how to send email, SMS, and notifications from the app. However, you can invoke any event-triggered task from the app using this approach. For example, you could invoke a task that uses a web hook to invoke an external web service.