Skip to content
Microsoft Dynamics 365

One of the very common customer ask regarding emails and workflow is how to send emails from workflow where the “from” field is set to some one other than the owner of the workflow rules. Two simple scenarios could be:



a) When a contact is created an automated thank you email should be sent from an alias such as helpdesk (queue).
b) When a contact is created an automated thank you email should be sent from the owner of the entity rather than workflow rule owner.


To achieve this, we first write a simple .net assembly that sends an email using a predefined email template to the requested party. Next, we use this .net assembly in the workflow rule to send the email.


Steps involved are:



1) Creating a .net assembly:




a. Create a sample project of name “CrmTest” whose output type is set to class library. I.e. it outputs a dll.
b. Add the web references to the project pointing to the crm server you have. The web reference url looks like: http://localhost/mscrmservices/2006/crmservice.asmx
c. Copy the below code in .cs file, compile the code and place the output dll into the assembly directory on the server.(C:\Program Files\Microsoft CRM\Server\bin\assembly)


using System;


using System.Text;


using System.Xml;


using CrmTest.localhost;


 


namespace CrmTest


{


      public class Test


      {


            public void SendTemplateEmailAs (


                  string callerXml,


                  Guid senderId,


                  string senderType,


                  Guid recipientId,


                  string recipientType,


                  Guid templateId)


            {


                  SendTemplateRequest request = new SendTemplateRequest();


                  // Set sender.


                  request.Sender = new Moniker();


                  request.Sender.Name = senderType;


                  request.Sender.Id = senderId;


 


                  // Set recipient.


                  request.RecipientType = recipientType;


                  request.RecipientIds = new Guid[] { recipientId };


 


                  // Set regarding.


                  request.RegardingType = recipientType;


                  request.RegardingId = recipientId;


 


                  // Set email template.


                  request.TemplateId = templateId;


 


                  // Create crm service.


                  CrmService svc = new CrmService();


                  svc.Credentials = System.Net.CredentialCache.DefaultCredentials;


                  svc.Timeout = -1;


                  // For auditing pupose set the callerId value.


                  svc.CallerIdValue = new CallerId();


                  // When seding as helpdesk set teh called id from the callerXml.


                  svc.CallerIdValue.CallerGuid = GetUserId(callerXml);


                  // When sending as the entity owner set the caller id to sender.


                  //svc.CallerIdValue.CallerGuid = senderId;


 


 


                  // Execute the request.


                  svc.Execute(request);


            }


 


            private Guid GetUserId(string callerXml)


            {


                  //Note: the callerXml is of the fomat:


                  //callerXml = “<caller><userid>684F160A-E4D9-4C3C-ADDD-1464D2B14EB2</userid><merchantid>F671739E-189A-4EE8-AF86-66DC8CF5F6C4</merchantid></caller>”;


                  XmlDocument xmldoc = new XmlDocument();


                  xmldoc.LoadXml(callerXml);


 


                  return new Guid(xmldoc.DocumentElement.SelectSingleNode(“//caller/userid”).InnerText);


            }


      }


}



2) Registering the .net assembly with workflow.




a. Registration for sending email as helpdesk (Queue)


<method name=”SendTemplateEmailAsHelpDesk” assembly=”CrmTest.dll” typename=”CrmTest.Test” methodname=”SendTemplateEmailAs” group=”Utility”>


            <parameter name=”callerXml” datatype=”caller”/>


            <parameter name=”SenderId” datatype=”lookup” entityname=”queue”/>


            <parameter name=”SenderType” datatype=”string” default=”queue”/>


            <parameter name=”RecipientId” datatype=”lookup” entityname=”contact”/>


            <parameter name=”RecipientType” datatype=”string” default=”contact”/>


            <parameter name=”TemplateId” datatype=”lookup” entityname=”template”/>


</method>




b. Registration for sending email as entity owner (system user)


<method name=”SendTemplateEmailAsEntityOwner” assembly=”CrmTest.dll” typename=”CrmTest.Test” methodname=”SendTemplateEmailAs” group=”Utility”>


            <parameter name=”callerXml” datatype=”caller”/>


            <parameter name=”SenderId” datatype=”lookup” entityname=”systemuser”/>


            <parameter name=”SenderType” datatype=”string” default=”systemuser”/>


            <parameter name=”RecipientId” datatype=”lookup” entityname=”contact”/>


            <parameter name=”RecipientType” datatype=”string” default=”contact”/>


            <parameter name=”TemplateId” datatype=”lookup” entityname=”template”/>


</method>



3) Constructing the workflow rule.




a) Workflow rule for sending email as Helpdesk (queue). Note you will have to create a queue with name helpdesk before creating the workflow rule.





b) Workflow rule for sending email as the entity owner.




4) Try it out :o).


Notes:
1) Ensure that the Bulk email service is running as the send email template users the bulk email service to send emails.
2) The invoking user should have “append to” privileges for queues for the workflow rule “Send as helpdesk” to work. During automatic invocation the rule owner credentials will be used and if invoked via the UI, the callers credential will be used.


Shashi Ranjan

We're always looking for feedback and would like to hear from you. Please head to the Dynamics 365 Community to start a discussion, ask questions, and tell us what you think!