Introduction
When creating and sending an email from Dynamics 365, the default functionality will populate the “From” field with the user creating the email. Depending on a user’s role within the organization, if they work on a team managing cases or other shared work, emails may need to all come from one email address. The user’s email address needs to be accurate in the system so we can’t change that, but we can use a queue to send emails rather than the user.
When creating an email from a case the email will be populated with the “Regarding” case. And, if the case is added to a queue we can retrieve the queue item and use the queue related to the case. Instead of populating the user in the “From” field, we can use JavaScript to set the queue in the “From” field when the form is loading.
In this post, we look at how we can do that.
Table of Content
- Prerequisites
- Setting up a Trial Environment
- The Goal of the Post
- How to Automatically Send Emails From a Queue
Prerequisites
- Familiar with JavaScript
- Familiar with Dynamics 365
- Familiar with the Power Platform
The Goal of the Post
In this post, we will create the functionality shown in the GIF below.

Setting up a Trial Environment
To create a Dynamics 365 CE trial environment you can follow the steps in the post, How to create Microsoft Dynamics 365 Customer Engagement Trial instance.
How to Automatically Send Emails From a Queue
In this part, we look at the steps we need to do to populate the email sender to a queue
Step 1: Go to https://make.powerapps.com.
Step 2: Choose your environment by clicking on Environment from the menu in the top right corner.

Step 3: Next, create a solution that we’re adding the necessary components. Click on the Solutions > + New Solution > Fill in Display Name, Name, Publisher, and Description > Click Create. As shown in the image below.

Step 4: Forward, we need to add the components we need in the solution we just created. In this case, we are going to add the existing email form and create a JavaScript web resource. To do this follow the steps below:
To add the email form click on Add existing > Tables (the steps continue after the image).

Furthermore, search for Email > Select Email in the list and click Next.

Click on Select objects.

Continue with, Click on Forms > Add the Email form > Then click Add.

Furthermore, you will see all the objects that are selected and are ready to be added to the solution. Click on Add to add the main form to the solution. Next, we look at how to create a JavaScript web resource.

Before we move on, a tip on how to find the form that is actively used on a table.
Nice to Know💡
To find the form that is actively used we can open an email in Dynamics 365, open the Developer Console and run the JavaScript code below in the console to get the Name and Id of the form that is open. As can be seen in the image below.
Xrm.Page.ui.formSelector.getCurrentItem();

Step 5: In this step, we will create a new JavaScript web resource. First, we’ll create a web resource in the solution we need to create the JavaScript file with the functionality locally on the machine. This is to be able to upload it in the web resource that we are going to create in the next step. To do this copy the script below and create a new file locally and save it as .js. As seen in the image below.

var sdk = window.sdk || {}; (function () { this.setQueueAsEmailSender = async function (executionContext) { // Get the form context const formContext = executionContext.getFormContext(); // Get the form type of loading record const formType = formContext.ui.getFormType(); // Check if the form is of type Create or Update if (formType === 1 || formType === 1) { // Get the Queue from the regarding record by using helper function const queue = await this._getRegardingQueue(formContext); // Check if Email has a regarding value if (queue !== null) { // Set the Queue as Sender in the email message formContext.getAttribute("from").setValue(queue); } } } this._getRegardingQueue = async function (formContext) { // Get regarding of the current email record const regardingObject = formContext.getAttribute("regardingobjectid").getValue(); // Check if Email has a regarding value if (regardingObject !== null) { // Get regarding record id const regardingObjectId = regardingObject[0].id.replace(/{|}/g, ""); // Retrieves the regarding record of the email const queueItemResult = await Xrm.WebApi.retrieveMultipleRecords("queueitem", `?$filter=_objectid_value eq ${regardingObjectId}`); // Check if the the result from WebApi query contains value by checking the length of the result if (queueItemResult.entities.length !== 0) { // Get values from Queue Item to find the Queue to set as sender const queueId = queueItemResult.entities[0]["_queueid_value"]; const queueEntityType = queueItemResult.entities[0]["_queueid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]; const queueName = queueItemResult.entities[0]["_queueid_value@OData.Community.Display.V1.FormattedValue"]; // Create a Queue Object to set as Sender in Email let queue = new Array(); queue[0] = new Object(); queue[0].id = queueId; queue[0].entityType = queueEntityType; queue[0].name = queueName; // Return queue object to main function return queue; } } // Return empty queue object to main function return null; } }).call(sdk);
After we have created the JavaScript file we will create the web resource. To do this, go back to the solution, and click on New > Web resource as seen in the image below.

Next, Upload the JavaScript file we created in the previous step and give the web resource a Display Name, Name, and Description, and click Save.

The solution now contains two components. A form and a web resource. Next, we are going to add JavaScript events to the email form.

Step 6: In this step, we are going to add an “On Load” and “On Change” event to the email form.
Click the ellipsis on the email component as shown in the image below > Open > Open in a new tab.

In the next window click on Forms in the Data experience section as seen in the image below.

After clicking on forms, click on the ellipses on the main form > Edit > Edit in new tab.

Next, we need to add the web resource to the form. We can do this by clicking the JS button in the left menu > Click + Add library > Search for the JavaScript Web Resource we created earlier > Select the Javascript > Click Add.

Now, we need to add the events to the form. We are going to add two events, “On Load” and “On Change”.
To add the “On Load” event > Click on Events > Select Event Type On Load > Select the web resource we added previously > Add the function call sdk.setQueueAsEmailSender (from the JavaScript file) > Check the box to Pass the execution context as first parameter > Click Done.

To add the “On Change” event, find and select the field Regarding and proceed with the same steps as above. Click on Events > Select Event Type On Change > Select the web resource we added previously > Add the function call sdk.setQueueAsEmailSender > Check the box to Pass the execution context as first parameter > Click Done.

Now, after we’ve added the events click on Save and then Publish.
Step 6: Time to test. Lastly, testing with one case that is added to a queue and another which is not.
If the case is added to a queue, the “From” field will be changed to the queue to which the case is added. See how it looks in the GIF below.

If the case is not added to a queue, the form field will be the user who created the email.

Altogether, that is pretty much it for this post. Hope you found it useful.