Home > CRM, Microsoft Dynamics > Microsoft Dynamics CRM: Sending email via workflow as different user

Microsoft Dynamics CRM: Sending email via workflow as different user

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

Advertisements
Categories: CRM, Microsoft Dynamics
  1. May 3, 2013 at 3:12 am

    There are some quite common blunders that men and women make when deciding upon
    the seo enterprise for their on the internet promoting desires.

    The research motor is less very likely to flag your one-way links as spam if they
    include things like a assortment of article content from other people also.

  2. May 6, 2013 at 3:11 am

    Wonderful beat ! I would like to apprentice while you amend your web site, how could
    i subscribe for a blog site? The account aided me a applicable deal.
    I were tiny bit acquainted of this your broadcast provided vibrant transparent idea

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: