Thursday, 16 April 2020

Sending an email from a Queue with Power Automate

When using the native email message capability of CDS or Dynamics 365, there are two options that can be used as the sender
  1. A system user - this is achieved from a licensed CDS or Dynamics 365 end user that also has the relevant Office 365 subscription 
  2. A queue - this is achieved from a queue that has been configured against a shared mailbox in Exchange

When would you use one over the other?

It depends on your business use case. If you have a team managing incoming emails where the entire team needs visibility, a queue is appropriate. This is typical in customer service centres or insurance claim teams where enquires, cases, support and so forth is emailed to a shared mailbox.

If you're a manager and you want to remind your colleagues in your team that they have not submitted their time sheet for dates of the week, a system user is appropriate.

In this WTF episode I'll be covering how to send an email from a Queue using flow in Power Automate as I've covered sending from a system user in previous WTF episodes.

Setting up your Queue in CDS or Dynamics 365

One of my friends who is also a Microsoft MVP, Megan Walker, has already done a blog post of how to set up a queue and send from a queue in classic workflows.

I'll reiterate her instructions in my own words and share what I usually do.

The steps I'm about to share is under the assumption that
  • the end user has the relevant licences for CDS or Dynamics 365 and Office 365
  • the end user has the relevant security role in CDS or Dynamics 365 to create a queue and set up the mailbox profile
  • the end user also has the relevant Office 365 permissions to set up a mailbox
If the end user's organisation has an IT ops or infrastructure team in-house (or even external party) then it's best for those teams to handle it in case they have policies set up. I have made the mistake in the past where I do it myself and when it comes to testing it doesn't work so then I hand it over to the organisation's in-house team or external party. Best to check first.

1. Create a shared mailbox in Office 365

The first thing I do is create a shared mailbox in Office 365 where I also add the relevant users to the mailbox as well.


For really good instructions on how to do this plus additional configuration, refer to this docs.microsoft.com article.

2. Configure the environment email settings

Head to Power Platform Admin Center and click on the . . . (aka ellipsis) against the environment you're going to be sending the email from. 


Expand the Business Settings and you'll see the Email Settings option. Select this option.


In the Outgoing Emails setting, select server-side synchronisation.


Cool, on to the next task.

3. Create a queue in CDS or Dynamics 365

Once again head to Power Platform Admin Center and click on the . . . (aka ellipsis) against the environment you need to create the Queue in. Expand the Business Settings and you'll see the option called Queues. Select this option.


Click on +New to create a new Queue record and fill in the fields.



For more configuration details of Queues please refer to this docs.microsoft.com article.

3. Configure the Shared Mailbox

In the Queue record, there will be a command called "Open Mailbox." 


Select this option which will launch a new window that displays the mailbox associated to the Queue. Set the Outgoing Email to Server-Side synchronisation and double check the email address is the same as the shared mailbox email address.


The final steps you want to do is
  1. Click Approve Email
  2. Click Test & Enable Mailbox

Head back to the Power Platform Admin Centre and head into Email Settings again. Select the Mailboxes option.


In here you should see that the mailbox has successfully been enabled.


If your shared mailbox profile in CDS or in Dynamics 365 was not successfully enabled, please raise a new support request through the Power Platform admin center for assistance with your environment.

4. Test your Shared Mailbox works

I don't stop after I configure the Shared Mailbox because experience has taught me that until you perform an outbound email from the Queue, it's not all green! I tripped up years ago when I didn't perform a test myself and since then have always tested my outbound emails after completing the set up and configuration of a Queue.

Create a new email message activity in CDS or Dynamics 365 and update the From field to the Queue you created. Also make sure you are selecting either a user (in other words a colleague) or a test contact as the recipient in the To field because if the email sends, it will send so do not use a real customer email address.


Hit send and the status should eventually update to Completed. Check with the recipient that they have received the email.

Sending from a Queue in a flow with Power Automate

Once the queue is all good to go, the queue can now be used as the sender for email messages. This is what my flow in Power Automate for this WTF episode looks like.

1.0 Trigger is when a Case is created

The trigger will be the same as WTF episode 21 where it will be when a Case is created.

Use the CDS List records action

The technique I shared with you in my WTF vlog is one I do use as it's one of those things that years have experience has taught me. Yes you could explicitly reference the GUID of the queue in your flow but that can result to deployment issues. Why? Chill for a couple of minutes and let me spill the tea #teatime

There may be times when a queue was not migrated from DEV to a target instance such as UAT. The queues will have different GUIDs which will cause either your flow in Power Automate or classic workflow to fail. To get around this in flow, use a CDS List records (current environment connector) with a query to the name of the queue. This means you need to create your flow in a solution in Power Automate to use the current environment connector.


I did warn during the WTF vlog this should work but there is a chance that for whatever reason the queue in the target instance may not have the same name as the originating instance. In this scenario you're going to have to use another type of query that will work for you.

Hack time

I feel like John Liu in this part of the blog post lol. This is #flowninja hack 115 😁
If you don't know what I am referring to, you must follow him on Twitter to understand his tweets on hacks he shares for flows in Power Automate 🐱‍👤💙

Currently there is a known issue where some entities will return a 200 status response instead of the full JSON response which makes it hard to validate if your CDS List records (current environment) action has correctly retrieve the correct record(s). My hack as a workaround right now is to use a Compose action and reference the body of the CDS List records (current environment) action. This will then give you the JSON response which you can review in Visual Studio Code or Notepad++ to confirm the correct Queue record was retrieved.

Final step - perform an unbound action

In WTF episode 21, the CDS action (current environment) used was the perform an unbound action where the action is SendTemplate. Same unbound action is used in this WTF episode and in the Sender field of the action, the queue is referenced instead of the system user. This is the difference when sending from a queue through a flow in Power Automate.


My expression to reference the queueid is

outputs('1.1_Retrieve_queues')?['body']?['value'][0]?['queueid']

This expression will prevent the Apply To Each from appearing which I covered in WTF episode 20.

Power Automate in action

Good to go, trigger the flow and away it goes in Power Automate! The sender will now be the Queue rather than a user.

Summary

Like classic workflows, queues can also be used as the sender of emails in flows with Power Automate which is what I shared in this WTF episode. The following is a checklist in order for queues to be used as the sender,
  1. A shared mailbox has been set up
  2. The email settings for the environment have been enabled to allow outbound emails
  3. A queue is created in Dynamics 365 and the mailbox profile has been configured to use the shared mailbox
  4. A test outbound email has been created and successfully sent
Thanks and don't forget to subscribe to my YouTube channel and hit the notification bell so that you don't miss out on my next WTF episode 🙂

0 comments:

Post a Comment