Handling Errors Action Sequences In Dataflows Gen2

Following on from my post two weeks ago about report bursting in Dataflows Gen2, I’ve learned something new about action sequences in M (thanks to Curt Hagenlocher for this, although it is mentioned in Ben Gribaudo’s post on actions) which deserves its own post.

Let’s take the first example I gave in that previous post showing how to send two emails using M in a Dataflow Gen2:

let
FirstEmail = AlertAction.Send(
MicrosoftOutlook.Actions(),
[
To = {"chris@someemail.com"},
Subject = "Email 1",
Body = "This is the first email"
]
),
SecondEmail = AlertAction.Send(
MicrosoftOutlook.Actions(),
[
To = {"helen@someemail.com"},
Subject = "Email 2",
Body = "This is the second email"
]
),
CreateSequence = Action.Sequence(
{FirstEmail, SecondEmail}
)
in
CreateSequence

This works, but what if the first email address in this sequence is invalid? I replaced the email on line 5 of the code snippet above with some text that is not an email address:

      To      = {"abcdef"},

When I tried to refresh the Dataflow it failed and no emails at all were sent – not even to the second, valid email address:

This behaviour makes sense for an action aequence where if one action fails then the whole sequence should fail, but for a reporting application you don’t want a single invalid email address to stop all emails being sent.

The solution is to use the Action.Try M function to wrap each individual action, like so:

let
FirstEmail = Action.Try(
AlertAction.Send(
MicrosoftOutlook.Actions(),
[
To = {"abcdef"},
Subject = "Email 1",
Body = "This is the first email"
]
)
),
SecondEmail = Action.Try(
AlertAction.Send(
MicrosoftOutlook.Actions(),
[
To = {"helen@someemail.com"},
Subject = "Email 2",
Body = "This is the second email"
]
)
),
CreateSequence = Action.Sequence(
{FirstEmail, SecondEmail}
)
in
CreateSequence

In this version of the Dataflow there is no error, the refresh succeeds but only one email is sent (to the second email, helen@someemail.com).

Leave a Reply