Yesterday, someone from a WhatsApp group asked me to help with the following scenario:
"I have created one team in Dynamics 365. I want to send an email to all the users when one of the checkboxes on the form is enabled.
(I am able to fetch the team name, but not able to fetch the users' email IDs of that group.) Any idea how to send an email to all the users of the team?"
To send an email to all users of a team in Dynamics 365 when a checkbox is enabled, you can follow these steps:
1. Understand Dynamics 365 Team and Users Relationship
- In Dynamics 365, each Team is associated with multiple Users.
- The relationship is typically stored in the TeamMembership table, linking Teams and Users.
2. Fetch Users' Email Addresses from the Team
To fetch the email addresses of users in a team, you can query the TeamMembership and SystemUser tables:
- Team Table: Get the Team's GUID.
- TeamMembership Table: Use the Team's GUID to retrieve associated User IDs.
- SystemUser Table: Fetch email addresses for those User IDs.
3. Use a Plugin, Workflow, or Power Automate
Option 1: Using a Plugin
If you're using a plugin, the logic can look like this:
Trigger Point:
- The plugin is triggered when the checkbox field is updated.
Plugin Code Sample:
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Fetch the team ID from the context or related entity
Guid teamId = new Guid("TEAM_GUID");
// Query to fetch users in the team
QueryExpression query = new QueryExpression("teammembership")
{
ColumnSet = new ColumnSet(false)
};
query.Criteria.AddCondition("teamid", ConditionOperator.Equal, teamId);
EntityCollection teamMemberships = service.RetrieveMultiple(query);
// Collect user emails
List emails = new List();
foreach (var teamMembership in teamMemberships.Entities)
{
Guid userId = teamMembership.GetAttributeValue("systemuserid");
Entity user = service.Retrieve("systemuser", userId, new ColumnSet("internalemailaddress"));
string email = user.GetAttributeValue("internalemailaddress");
if (!string.IsNullOrEmpty(email))
{
emails.Add(email);
}
}
// Send email logic
// Create and send email using the collected email addresses
}
Option 2: Using Power Automate
- Trigger: Set the trigger as When a record is updated for the entity containing the checkbox.
- Actions:
- Use the team ID to fetch the team record.
- List team members using the List Rows action on the
TeamMembershiptable with theteamidfilter. - Fetch user emails from the
SystemUsertable. - Send an email using the collected email addresses.
Option 3: Using a Workflow
- Trigger: The workflow is triggered when the checkbox is updated.
- Steps:
- Retrieve users in the team using the
RetrieveMultiplemethod. - Send an email to all retrieved users.
- Retrieve users in the team using the
No comments:
Post a Comment