Monday, 13 May 2019

How to Send Birthday Email to Contacts using Custom Workflow in Dynamic CRM

How to Send Birthday Email to Contacts using Custom Workflow in Dynamic CRM


Description:


In this example, we explain that how to Sending an automated email to a Contact on their birthday, a simple requirement that can be achieved with Dynamics 365 Custom Workflow.
Generally will get the requirement from client to send Birthday emails to their customers. Everyone will suggest going with Console Application schedule to run at the specific time but that is not the best solution so here we demonstrate to how to send Birthday Email to client or customer by using Workflow in Dynamic 365 CRM.


Below is the code Library and step to sending birthday email in Dynamic CRM.
1. Create a custom entity Birthday.

     - Add one date time field Next Call to keep tomorrows date in order to start workflow every date recurring.

2. Create a Custom Workflow Birthday Alert
    - It will check birthday contacts for today and create one email record for respected contacts.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Activities;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;

namespace Birthday
{
    
public class Birthday : CodeActivity
    {
        
protected override void Execute(CodeActivityContext context)
        {
            
//Tracing service
            
ITracingService tracing = context.GetExtension<ITracingService>();

            
//workfow context
            
IWorkflowContext WorkflowContext = context.GetExtension<IWorkflowContext>();
            
IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
            
IOrganizationService service = serviceFactory.CreateOrganizationService(WorkflowContext.UserId);

            tracing.Trace(
"Birthday Workflow started");

            
//Query to get todays birthday contacts
            
QueryExpression birthdayContactsQuery = new QueryExpression
            {
                EntityName = 
"contact",
                ColumnSet = 
new ColumnSet("contactid""firstname"),
                Criteria = 
new FilterExpression
                {
                    Conditions = 
                        {
                            
new ConditionExpression 
                            {
                                AttributeName = 
"birthdate",
                                Operator = 
ConditionOperator.Equal,
                                Values = { 
DateTime.Today }
                            }
                        }
                }
            };

            
DataCollection<Entity> birthdayContacts = service.RetrieveMultiple(birthdayContactsQuery).Entities;

            
foreach (Entity contact in birthdayContacts)
            {
                
//Create email
                
Entity email = new Entity("email");

                
Entity Fromparty = new Entity("activityparty");
                Fromparty.Attributes.Add(
"partyid"new EntityReference("systemuser", WorkflowContext.UserId));
                
Entity Toparty = new Entity("activityparty");
                Toparty.Attributes.Add(
"partyid"new EntityReference("contact", contact.Id));

                email[
"from"] = new Entity[] { Fromparty };
                email[
"to"] = new Entity[] { Toparty };
                email[
"directioncode"] = true;
                email[
"subject"] = "Wish you very Happy Birthday";

                
// Create the request
                
SendEmailFromTemplateRequest emailUsingTemplateReq = new SendEmailFromTemplateRequest
                {
                    Target = email,
                    TemplateId = 
new Guid(TemplateID.Get(context).ToString()), //Guid("9CB6EC15-D3C2-E211-B090-00155D07ECE3"),
                    RegardingId = contact.Id,
                    RegardingType = contact.LogicalName
                };

                
SendEmailFromTemplateResponse emailUsingTemplateResp = (SendEmailFromTemplateResponse)service.Execute(emailUsingTemplateReq);

                
//Update next birthday
                contact[
"birthdate"] = DateTime.Today.AddYears(+1);
                service.Update(contact);
            }

            tracing.Trace(
"email send successfully");
        }

        
//input parameters
        [
Input("Template ID")]
        [
RequiredArgument]
        
public InArgument<string> TemplateID { getset; }

        [
Input("frequently")]
        [
Default("1")]
        
public InArgument<string> Frequently { getset; }
    }
}

- register the assembly.
-create a workflow Send Birthday Mail.
-on update of Next Cell call the workflow.
-Select wait condition (timeout equal Next Call).
-Publish the workflow.

3. Create birthday record. Workflow will start and it will start sending emails.

1 comments:

  1. Your Affiliate Money Printing Machine is waiting -

    And making money with it is as easy as 1, 2, 3!

    This is how it all works...

    STEP 1. Choose affiliate products the system will advertise
    STEP 2. Add some PUSH BUTTON traffic (it LITERALLY takes JUST 2 minutes)
    STEP 3. See how the system explode your list and up-sell your affiliate products all on it's own!

    Are you ready to start making money?

    You can test-drive the system for yourself risk free...

    ReplyDelete