Sunday, May 22, 2011

Microsoft Dynamics CRM 2011 : How to send Email Notificaiton on Record Sharing?


Solution: Workflows are very useful in Microsoft Dynamics CRM 2011, can be used to send automated email messages, create, update, assign records etc.. but can only be triggered on record creation, status change, assignment, attribute change and deletion and what if it is required to trigger workflow on entity SDK message processing i.e. record sharing etc. For such requirements, i am taking an example of sending email notifications on lead record sharing, You can follow the steps below for sending email notification on lead record sharing.

1.    Create two new attributes in lead entity
    
     a. new_IsSharedRecord (Type = Bit/Two Options)
    
     b. new_LastSharedDateTime (Type = DateTime)

2.    Create plugin that will be triggered on “GrantAccess” message of lead entity and update “new_isSharedRecord”  attribute value to “True” and also store current system date time in “new_LastSharedDateTime”

3.    Create new workflow that will be triggered when “new_isSharedRecord” attribute value will be changed

4.    Check if attribute “new_isSharedRecord” = true then execute below steps

a.    Update attribute value “new_isSharedRecord” = false

b.    Send email message



[Plugin Code]


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Crm.Sdk;

namespace wod.Crm.LeadShareAlert
{
    public class wodPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

            IOrganizationServiceFactory wod_serviceFactory = null;

            IOrganizationService wod_CrmService = null;

            try
            {
                wod_serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                wod_CrmService = wod_serviceFactory.CreateOrganizationService(context.UserId);

                if (context.InputParameters.Contains("Target")
                && context.InputParameters["Target"] is EntityReference)
                {
                    switch (context.MessageName)
                    {
                        case "GrantAccess":

                            EntityReference wod_PluginEntity = (EntityReference)context.InputParameters["Target"];

                            if (wod_PluginEntity.LogicalName == "lead")
                            {
                                Entity wodLead = new Entity(wod_PluginEntity.LogicalName);

                                wodLead.Attributes.Add("leadid", wod_PluginEntity.Id);
                                wodLead.Attributes.Add("new_issharedrecord", true);
                                wodLead.Attributes.Add("new_lastshareddatetime", DateTime.Now);

                                wod_CrmService.Update(wodLead);
                            }

                            break;
                    }
                }

            }

            catch (System.Web.Services.Protocols.SoapException ex)
            {
                throw new InvalidPluginExecutionException(ex.Detail.InnerText);
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
    }
}





3 comments:

  1. Dear Jehanzeb

    I had tried the above code but there are no emails sent. The receiptent remains empty.

    Kindly help me sort this out

    Regards
    Harsh.P.Ajgaonkar

    ReplyDelete
  2. Have you heard of this fabulous way of life! After all several small changes will, as it diet pills to help lose weight helps
    in reducing weight in a good eating habits and lifestyle to lose
    weight fast, easy as looking into all of that
    weight gain and to work. Our metabolism gets a boost in energy.

    You will need to cut it out over many small meals per day.


    My blog: what is the best over counter diet pill

    ReplyDelete
  3. Workflows are very useful in Microsoft Dynamics CRM 2011, can be used to send automated email messages, create, update, assign records etc.. but can only be triggered on record creation, status change, assignment, attribute change and deletion and what if it is required to trigger workflow on entity SDK message processing
    pakistani suits wholesale in pakistan ,
    pakistani suits wholesale ,

    ReplyDelete