locked
Create an email activity with a custom workflow RRS feed

  • Question

  • Hi everyone,

    I'm really fresh on CRM and Workflow Windows Foundation but I absolutely need to create a custom workflow that send an email notification when a new leads is inserted on CRM.

    So I'm able to register, publish, trigger and debug the workflow I created.

    But I really don't know how do I do in order to create an email activity, to send the email and to attach the email sent to the lead inserted.

    Could you please help me with a code sample?
    Could you please let me know with class I have to use in order to implement the email activity and send the email?

    thanks a lot in advance to everyone wants help a desperate guy :(

    Wednesday, September 23, 2009 2:21 PM

Answers

  • Hi, Nicola.

    Try following code:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Crm.Workflow;
    using System.Workflow.Activities;
    using System.Workflow.ComponentModel;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using System.ServiceModel;
    using System.Security.Principal;
    
    namespace SendReportAction
    {
        [CrmWorkflowActivity("Execute and send a report")]
        public class SendReport : SequenceActivity
        {
            protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
            {
                if (MailRecipient != null && !MailRecipient.IsNull && !MailRecipient.IsNullSpecified)
                {
                    IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
                    IWorkflowContext workflowContext = contextService.Context;
                    ICrmService crmservice = workflowContext.CreateCrmService();
    
                    email mail = new email();
    
                    activityparty fromparty = new activityparty();
                    fromparty.partyid = new Lookup();
                    fromparty.partyid.type = EntityName.systemuser.ToString();
                    fromparty.partyid.Value = workflowContext.UserId;
                    mail.from = new activityparty[] { fromparty };
    
                    activityparty toparty = new activityparty();
                    toparty.partyid = new Lookup();
                    toparty.partyid.type = EntityName.systemuser.ToString();
                    toparty.partyid.Value = MailRecipient.Value;
                    mail.to = new activityparty[] { toparty };
    
                    mail.subject = "Report Subscription";
                    mail.sender = "crm@example.com";
    
                    mail.description = "Report Subscription";
    
                    mail.ownerid = new Owner();
                    mail.ownerid.type = EntityName.systemuser.ToString();
                    mail.ownerid.Value = workflowContext.UserId;
                    Guid createdEmailGuid = crmservice.Create(mail);
    
    
                    SendEmailRequest sendrequest = new SendEmailRequest();
                    sendrequest.EmailId = createdEmailGuid;
                    sendrequest.TrackingToken = "";
                    sendrequest.IssueSend = true;
    
                    crmservice.Execute(sendrequest);
                }
    
                return ActivityExecutionStatus.Closed;
            }
    
    
            public static DependencyProperty MailRecipientProperty = DependencyProperty.Register("MailRecipient", typeof(Lookup), typeof(SendReport));
    
            [CrmInput("MailRecipient")]
            [CrmReferenceTarget("systemuser")]
            public Lookup MailRecipient
            {
                get
                {
                    return (Lookup)base.GetValue(MailRecipientProperty);
                }
                set
                {
                    base.SetValue(MailRecipientProperty, value);
                }
            }
        }
    }



    Full article here .
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Wednesday, September 23, 2009 2:54 PM
    Moderator
  • Hi, Nicola.

    You can make this action using standard workflow wizards (check screen-shots):

    Worflow's Creation
    Step's Addition
    Step is added
    Email's fields filling in
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    • Marked as answer by NicolaSpin Thursday, April 22, 2010 7:18 PM
    Wednesday, September 23, 2009 2:38 PM
    Moderator

All replies

  • Hi,

    You don't need to use custom code in order to send emails via workflow. I would suggest using built-in mechanisms, because it is much easier and faster to deploy. Here you may find some good info on how to send emails on particular event with the "clickable" workflow. Just create a workflow on the "Create" event on the "Lead" entity.

    I hope that helps,
    Kuba
    -- Kuba Skałbania, Netwise
    Wednesday, September 23, 2009 2:33 PM
  • Might help if you shared why you didn't use normal workflow over custom...
    Wednesday, September 23, 2009 2:33 PM
  • Hi, Nicola.

    You can make this action using standard workflow wizards (check screen-shots):

    Worflow's Creation
    Step's Addition
    Step is added
    Email's fields filling in
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    • Marked as answer by NicolaSpin Thursday, April 22, 2010 7:18 PM
    Wednesday, September 23, 2009 2:38 PM
    Moderator
  • hello,

    Step 1:
    go to Settings-->workflow --> click on New

    Give Workflow name and select entity as Lead

    configure scope of the email as per your requirement and select to run this workflow when record is created (becuase you want to send email when new leads is insterted in to CRM)

    refer example screen print at http://drop.io/p6p0wsn/asset/step-1-jpg

    Step 2 :
    after adding Send Email Step now configure your email properties by clicking Set Properties button..

    refer example screen print at http://drop.io/e9sxitw/asset/step-2-jpg


    That's it done!! hope this helps ..
    Wednesday, September 23, 2009 2:42 PM
    Answerer
  • Hi Andriy and Maya,

    first of all thanks a lot for your reply.

    But unfortunately I need to make a custom workflow because i have to put inside some custom logic.  :(

    so I really need to implement a custom one and ot obtain an email activity attached to the lead that has triggered the wf.

    Could you please help me in that direction?

    thanks a lot for above and in advanced for futher tips...

    Wednesday, September 23, 2009 2:50 PM
  • I cannot use a wizard workflow because i need to keep emailTo\CC\Bcc from a separeted table and also because Email template changes often following a custom logic.

    Thanks again for your support
    Nicola
    Wednesday, September 23, 2009 2:52 PM
  • Hi, Nicola.

    Try following code:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Crm.Workflow;
    using System.Workflow.Activities;
    using System.Workflow.ComponentModel;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using System.ServiceModel;
    using System.Security.Principal;
    
    namespace SendReportAction
    {
        [CrmWorkflowActivity("Execute and send a report")]
        public class SendReport : SequenceActivity
        {
            protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
            {
                if (MailRecipient != null && !MailRecipient.IsNull && !MailRecipient.IsNullSpecified)
                {
                    IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
                    IWorkflowContext workflowContext = contextService.Context;
                    ICrmService crmservice = workflowContext.CreateCrmService();
    
                    email mail = new email();
    
                    activityparty fromparty = new activityparty();
                    fromparty.partyid = new Lookup();
                    fromparty.partyid.type = EntityName.systemuser.ToString();
                    fromparty.partyid.Value = workflowContext.UserId;
                    mail.from = new activityparty[] { fromparty };
    
                    activityparty toparty = new activityparty();
                    toparty.partyid = new Lookup();
                    toparty.partyid.type = EntityName.systemuser.ToString();
                    toparty.partyid.Value = MailRecipient.Value;
                    mail.to = new activityparty[] { toparty };
    
                    mail.subject = "Report Subscription";
                    mail.sender = "crm@example.com";
    
                    mail.description = "Report Subscription";
    
                    mail.ownerid = new Owner();
                    mail.ownerid.type = EntityName.systemuser.ToString();
                    mail.ownerid.Value = workflowContext.UserId;
                    Guid createdEmailGuid = crmservice.Create(mail);
    
    
                    SendEmailRequest sendrequest = new SendEmailRequest();
                    sendrequest.EmailId = createdEmailGuid;
                    sendrequest.TrackingToken = "";
                    sendrequest.IssueSend = true;
    
                    crmservice.Execute(sendrequest);
                }
    
                return ActivityExecutionStatus.Closed;
            }
    
    
            public static DependencyProperty MailRecipientProperty = DependencyProperty.Register("MailRecipient", typeof(Lookup), typeof(SendReport));
    
            [CrmInput("MailRecipient")]
            [CrmReferenceTarget("systemuser")]
            public Lookup MailRecipient
            {
                get
                {
                    return (Lookup)base.GetValue(MailRecipientProperty);
                }
                set
                {
                    base.SetValue(MailRecipientProperty, value);
                }
            }
        }
    }



    Full article here .
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Wednesday, September 23, 2009 2:54 PM
    Moderator
  • Thanks a lot Andriy,

    Now I able to send the email!!!! great really really thanks....

    so Now I need to attach the email activity created to the lead that has triggered the workflow in order to keep track of the email sent and receivd.

    So, my questions are:
    - How di I do attach the email activity to the lead tha has triggered the workflow? with the regardingID? could you please give me a code example?
    - Ho can I postpone the email send? after 1 hour for example?

    I wanna say really thanks for your help,

    Nicola
    Wednesday, September 23, 2009 3:49 PM