web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Email from workflow to more recipients including invalid one

(0) ShareShare
ReportReport
Posted on by

Scenario: After some operation on Incident/Case (e.g. Status change), we need to send an email to the list of recipients: Owner, Created by, Modified by, and Contact. However, if some of them has an invalid email address, the message is not send to anybody.

I am afraid this is by-design. We would like the email to be send to all recipients with a valid email address. I can imagine some workarounds, but none of them seems 100%:

1)      Sending more emails, every with a single recipient. This seems hard to maintain (duplicities) and making mess in activities. Moreover, a recipient doesn’t see the others.

2)      Ensure everybody has a proper email address. Not possible. We have a few users without an email address (we could use some mock one), contacts can temporarily have no address and there is also the SYSTEM account.

3)      Do it by plugin. Possible, but quite complex. We lose possibility for wysiwig email editor, and quick/easy workflow modification.

Is there some other way to solve this? Thanks!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aiden Kaskela Profile Picture
    19,696 on at

    Hi Jiri,

    In step 1 or 2, couldn't you add workflow checks on the different users? This would make it quite easy to do in a workflow.

    With #3, you can still use the wysiwyg editor in a plugin (this is how I send scheduled emails via code). In your plugin, you'll create and send the email. To leverage the template, in code, retrieve the template you want and set the fields you want from the template: email.subject = template.subject and email.description = template.description. Then to send the email you'd use a SendEmailRequest.

    Hope this helps! I'd appreciate if you'd mark this as Answering your question.

    Thanks,

      Aiden

  • Community Member Profile Picture
    on at

    If we have four recipients, and actually everyone can be missing an email address; then it is 4 x Yes/No = 16 possible combinations of recipients, or conditions. And it is needed in more workflows. It doesn’t seem like easily managed customization.

    I like the option using Template in plugin. Although, we will need to trash the existing workflows. I’ll try it and I’ll get back to your answer. Thanks!

  • Aiden Kaskela Profile Picture
    19,696 on at

    I misread your original post, I thought they all needed an email or else none would receive it. Good luck with the plugin, leery me know if you hit any problems.

    Thanks,

     Aiden

  • Verified answer
    Community Member Profile Picture
    on at

    After some testing, we've decided for another approach - deleting invalid recipients from email before sending it by plugin. There are some conditions - it is done only for system emails.

    Pros: We can keep existing workflows. We can continue using workflows. We don't mess up workflows or a plugin with many conditions.

    The code:

    private void checkActivityParties(IOrganizationService service, Entity email)
    {
        String[] addreseeFields = { "to", "cc", "bcc" };
        ColumnSet cs = new ColumnSet(addreseeFields);
        if (!Util.containsSomeOfAttributes(email, cs)) { return; }
        cs.AddColumns("createdby");
        Entity completeEmail = service.Retrieve("email", email.Id, cs);
    
        if (completeEmail.GetAttributeValue<EntityReference>("createdby").Id != Util.systemuUserId
            && completeEmail.GetAttributeValue<EntityReference>("createdby").Id != Util.crmAdminId
            ) { return; }
    
        Dictionary<String, String> emailFields = new Dictionary<String, String>() {
                {"contact","emailaddress1" },
                {"account","emailaddress1" },
                {"lead","emailaddress1" },
                {"systemuser","internalemailaddress" },
                {"queue","emailaddress" },
                {"team","emailaddress" },
            };
    
        int totalRecipients = 0;
    
        foreach (String addreseeField in addreseeFields)
        {
            EntityCollection oldAddresees = completeEmail.GetAttributeValue<EntityCollection>(addreseeField);
            List<Entity> newAddresees = new List<Entity>();
    
            foreach (Entity addresee in oldAddresees.Entities)
            {
                EntityReference partyRef = addresee.GetAttributeValue<EntityReference>("partyid");
    
                if (!emailFields.ContainsKey(partyRef.LogicalName)) { continue; }
                String emailField = emailFields[partyRef.LogicalName];
    
                Entity addresseeEntity = service.Retrieve(partyRef.LogicalName, partyRef.Id, new ColumnSet(emailField));
                if (addresseeEntity.Contains(emailField))
                {
                    newAddresees.Add(addresee);
                }
            }
    
            if (newAddresees.Count < oldAddresees.Entities.Count)
            {
                Entity updatedEmail = new Entity("email");
                updatedEmail.Id = email.Id;
                updatedEmail[addreseeField] = newAddresees.ToArray();
                service.Update(updatedEmail);
            }
    
            totalRecipients += newAddresees.Count;
        }
    
        if (totalRecipients == 0)
        {
            service.Delete("email", email.Id);
        }
    
    }


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans