Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
Creating a custom workflow (email activity)

Pergunta Creating a custom workflow (email activity)

  • segunda-feira, 20 de agosto de 2012 13:46
     
      Contém Código

    Hello,

    I am trying to send an email to the customer (contact) using their email address on the Order form. Workflow consists of: If the order is greater than 5000. Send an email to the customer to take our survey. Issue is I just can't add email address to the To: field. I want to send email to a contact, not the account contact. Issue I am having, CRM doesn't support out of the box. So, I have create a custom workflow. I found some partial code on the web that I think can be useful. This is my first time creating a customer workflow for CRM 4.0. Here is the code i have:

    using System;
    using System.Collections;
    using System.Workflow.ComponentModel.Compiler;
    using System.Workflow.ComponentModel.Serialization;
    using System.Workflow.ComponentModel;
    using System.Workflow.ComponentModel.Design;
    using System.Workflow.Runtime;
    using System.Workflow.Activities;
    using System.Workflow.Activities.Rules;
    using System.Reflection;
    
    using Microsoft.Crm.Workflow;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk.Query;
    using CrmService;
    
    
    
    namespace SampleWorkflows
    {
    	class EmailActivity : Activity
    	{
    
    
           public static void sendEmail(string userGuid, string emailSubject, string emailBody, string recipientType) 
    
            {  
               
                //Set up CRM service
                CrmService.CrmService crmservice = GetCrmService();
    
                // Create a FROM activity party for the email.
                activityparty fromParty = new activityparty();
                fromParty.partyid = new Lookup();
                fromParty.partyid.type = EntityName.systemuser.ToString();
                fromParty.partyid.Value = new Guid(/*guid of sending user*/);
    
                //Create a TO activity party for email
                activityparty toParty = new activityparty();
                toParty.partyid = new Lookup();
                toParty.partyid.type = EntityName.contact.ToString();
                toParty.partyid.Value = new Guid(userGuid);
    
                //Create a new email
                email emailInstance = new email();
    
                //set email parameters
                emailInstance.from = new activityparty[] { fromParty };
                emailInstance.to = new activityparty[] { toParty };
                emailInstance.subject = emailSubject;
                emailInstance.description = emailBody;
    
                //Create a GUId for the email
                Guid emailId = crmservice.Create(emailInstance);
    
                //Create a SendEmailRequest
                SendEmailRequest request = new SendEmailRequest();
                request.EmailId = emailId;
                request.IssueSend = true;
                request.TrackingToken = "";
    
                 //Execute request
                crmservice.Execute(request);
            }
    
    
    
    	}
    }
    

    I get an error specifying: the type or namespace name 'CRMService' could not be found (are you misssing a using directive or an assembly reference.

    Can anyone point if I am on the right track on creating this custom workflow and point any mistakes? I 'll appreciate all your help.

    Thanks,

    Jose

Todas as Respostas

  • segunda-feira, 20 de agosto de 2012 13:53
     
     

    Hi Jose,

    Are you sure the same error is not coming while you build workflow project ?

    From error it seems helper class CRMService you've used is not built into workflow assembly.


    Thanks and regards,
    Hiren Solanki.
    Please vote or mark as a answer if it helped you.

  • segunda-feira, 20 de agosto de 2012 14:07
    Moderador
     
      Contém Código

    It looks like your code mixes use of the CRM SDK assemblies, and a web reference to the CRM web services. You should only use one of these, and in a custom workflow activity, you should only use the CRM SDK assemblies.

    Another issue is that you need to override the Execute method, which will pass you an ActivityExecutionContext.

    The changes you need to make are:

    1. Remove the line: using CrmService;   -- This relates to a web reference that you don't have, and don't need
    2. Override the Execute method
    3. Within the Execute method, use the code below to instantiate your crmservice instance, replacing CrmService.CrmService crmservice = GetCrmService();
    4. You'll also need to setup parameters for the Subject, Body etc.
    // Get the context service.
       IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
       IWorkflowContext context = contextService.Context;
    
       // Use the context service to create an instance of CrmService.
       ICrmService crmcervice = context.CreateCrmService();


    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk


  • segunda-feira, 20 de agosto de 2012 18:31
     
     

    Hello David,

    Thanks for your help! I am currently updating my solution based on your feedback. I let you know how it goes. I post my code once I have finish updating it.

    Jose

  • segunda-feira, 20 de agosto de 2012 20:47
     
     

    Hello David,

    I fairly new to working with workflows. Is there anyway you can give me an example of settingup the parameter. I only need to modify email field to enter a dynamic value.

    Thanks


    Jose Fleitas

  • terça-feira, 21 de agosto de 2012 08:49
    Moderador
     
     

    Jose

    The samples in the SDK (http://msdn.microsoft.com/en-us/library/cc151229 ) should show you what you need

    David


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk