locked
Crm 2011 :Workflow - Sending email problem. RRS feed

  • Question

  • Goal:

    Send email through a workflow but with a custom field of type text email without having to create a contact/account first.

    Problem and what i want to achieve:

    As it is now if you create a workflow with the email step, you have to fill in the To field of the step with a customer. The thing i want to do is just give the email address(text field in custom entity ) and send a email with the address that is stated in the custom text field of the custom entity.

    Can that be achieved  with just only the email address and nothing else and if so how and with what(workflow,custom workflow,plugin,...)?

    Enviroment:

    CRM 2011 On-Premise,Server OS: Windows Server 2008 R2 64bit, Client OS: Windows 7 64bit, email router not installed, SQL SErver 2008, different servers for crm and sql,Office 2007, not the final production system but a development sytem.


    Please vote if I helped out :) thanks all in this forum for helping with any problem i had.
    • Edited by gokou84san Friday, May 6, 2011 6:01 AM
    Thursday, May 5, 2011 1:37 PM

Answers

  • Sample code for more help

    SendEmailRequest req = new SendEmailRequest();
    req.EmailId = emailId;//ID of created mail
    req.TrackingToken = "";
    req.IssueSend = true;

    SendEmailResponse res = (SendEmailResponse)service.Execute(req);


    Thanks, Ankit Shah
    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html
    Friday, May 6, 2011 2:39 PM

All replies

  • hi,

    Out of the box the To field in an email accepts only Account, contact, Facility/Equipment, Lead, Queue and User.

    So in order to achieve the goal , you will have to write a custom workflow activity (optional : taking the custom text field in your input  parameter )and refer it in your workflow.

     



    vishal swami

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    http://msdynamics4you.blogspot.com
    Thursday, May 5, 2011 2:11 PM
  • hi,

    Out of the box the To field in an email accepts only Account, contact, Facility/Equipment, Lead, Queue and User.

    So in order to achieve the goal , you will have to write a custom workflow activity (optional : taking the custom text field in your input  parameter )and refer it in your workflow.

     



    vishal swami

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    http://msdynamics4you.blogspot.com

    Yeah i imagined that but again wont i have to use the email entity to send an email in the custom workflow? i dont know how to send an email through a custom workflow but i guess it will need to fill in the To field again that requires the above CRM entiites
    Please vote if I helped out :) thanks all in this forum for helping with any problem i had.
    Thursday, May 5, 2011 2:15 PM
  • How can i send an email from a custom workflow using a custom text field email address for the To field and niot CRM enitites?
    Please vote if I helped out :) thanks all in this forum for helping with any problem i had.
    Friday, May 6, 2011 6:03 AM
  • How can i send an email from a custom workflow using a custom text field email address for the To field and niot CRM enitites?
    Please vote if I helped out :) thanks all in this forum for helping with any problem i had.


    Anyone?

     


    Please vote if I helped out :) thanks all in this forum for helping with any problem i had.
    Friday, May 6, 2011 2:12 PM
  • You could use SendEmailRequest class of SDK to send an email prgrammatically.

    Refer below link for more details.

    http://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.sendemailrequest_members.aspx


    Thanks, Ankit Shah


    Inkey Solutions, India.


    Microsoft Certified Business Management Solutions Professionals


    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html

    Friday, May 6, 2011 2:33 PM
  • Sample code for more help

    SendEmailRequest req = new SendEmailRequest();
    req.EmailId = emailId;//ID of created mail
    req.TrackingToken = "";
    req.IssueSend = true;

    SendEmailResponse res = (SendEmailResponse)service.Execute(req);


    Thanks, Ankit Shah
    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html
    Friday, May 6, 2011 2:39 PM
  • Sample code for more help

    SendEmailRequest req = new SendEmailRequest();
    req.EmailId = emailId;//ID of created mail
    req.TrackingToken = "";
    req.IssueSend = true;

    SendEmailResponse res = (SendEmailResponse)service.Execute(req);


    Thanks, Ankit Shah
    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html


    I dont undderstand how can i send with this the email address a have in a custom field in a custom entity without having to create a record of that entity?

    Where is the email address given to send?

     

    As i stated the scenario i have is like so: I have a custom field in custome entity that is for the email address. From custom entity A (which has the email address field), with a workflow i create a new record for custom entity B with the same fields. When I create B I want to send an email with the custom fields email address that exists in A and B now. How can i do that?


    Please vote if I helped out :) thanks all in this forum for helping with any problem i had.
    Monday, May 9, 2011 6:12 AM
  • Hi,

    You need to register a plug-in on entity B post create. So you will have the both the records before sendin an email to them, both of custom entity A and B.

    Now in Pugin you need to fetch a custom field emal address assoicated with entity A and send an email by setting email in above code

    req.EmailId = emailId;//ID of created mail

    I assume that both the email address are same for Entity A and B then you need to send the email only once. Please clarify if I misundstood anything.

     

     


    Thanks, Ankit Shah
    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html
    Monday, May 9, 2011 6:29 AM
  • Hi,

    You need to register a plug-in on entity B post create. So you will have the both the records before sendin an email to them, both of custom entity A and B.

    Now in Pugin you need to fetch a custom field emal address assoicated with entity A and send an email by setting email in above code

    req.EmailId = emailId;//ID of created mail

    I assume that both the email address are same for Entity A and B then you need to send the email only once. Please clarify if I misundstood anything.

     

     


    Thanks, Ankit Shah
    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html


    yes you understood exactly. So if a fetch the email address text field from entity A it will give me an ID that i will assign it to the req.EmailId line , right? Or this email id will be the id of the created email entity record?

     

    so in entity B i will have something like :

     

     SendEmailRequest req = new SendEmailRequest();
     req.EmailId = <EmailID fetched from the entity A email address text field>;//ID of created mail
     req.TrackingToken = "";
     req.IssueSend = true;
    
    

     

    Thanks


    Please vote if I helped out :) thanks all in this forum for helping with any problem i had.
    Monday, May 9, 2011 7:21 AM
  • Yes, the fetched email Id of entity A needs to be used in B.
    Thanks, Ankit Shah
    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html
    Monday, May 9, 2011 7:25 AM
  • Yes, the fetched email Id of entity A needs to be used in B.
    Thanks, Ankit Shah
    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html


    The only thing that  i am confussed with is that an email field doesn't it return a string value and not an guid?  the field in entity A and B are both the same and they are both email fields of type Single Line of text of the format Email. Doesnt this return a string and not a guid?

     


    Please vote if I helped out :) thanks all in this forum for helping with any problem i had.
    Monday, May 9, 2011 7:40 AM
  • Yes it will return you a string email address which could be used further.
    Thanks, Ankit Shah
    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html
    Monday, May 9, 2011 7:49 AM
  • Thanks ankit. It worked fine.

    ;)


    Please vote if I helped out :) thanks all in this forum for helping with any problem i had.
    Monday, May 9, 2011 7:57 AM
  • Hi Ankit,

    I tried using the sample code you provided above, but I keep getting an "Object reference not set to an instant of an object" exception when I try to execute the SendEmailRequest. The email is created without any problems, but it just won't send it, for some weird reason.

    The code is literally identical to that of the SDK sample, only I run it in context of a plugin rather than a console app.

    Any ideas what I might be doing wrong?

     

    Thanks,

    Maria

    Wednesday, June 22, 2011 11:32 AM
  • @Maria,

    Could you please check the system event viewer or the CRM trace log to investigate further?

     

     

     


    Thanks, Ankit Shah
    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html
    Wednesday, June 22, 2011 1:31 PM
  • I get the following from the event viewer:

    - <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">

    - <System>

      <Provider Name="MSCRMWebService" /> 

      <EventID Qualifiers="49152">18176</EventID> 

      <Level>2</Level> 

      <Task>0</Task> 

      <Keywords>0x80000000000000</Keywords> 

      <TimeCreated SystemTime="2011-06-22T13:40:34.000000000Z" /> 

      <EventRecordID>12994</EventRecordID> 

      <Channel>Application</Channel> 

      <Computer>dkcrm99.KonicaMinolta.dk</Computer> 

      <Security /> 

      </System>

    - <EventData>

      <Data>6c330764-4c97-e011-9a6b-00155dc9837e</Data> 

      <Data>05cdbb1b-ea3e-db11-86a7-000a3a5473e8</Data> 

      <Data>email</Data> 

      <Data>30</Data> 

      <Data>Send</Data> 

      <Data>Microsoft.Crm.Extensibility.InternalOperationPlugin, Microsoft.Crm.ObjectModel, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35</Data> 

      <Data>Microsoft.Crm.Extensibility.InternalOperationPlugin</Data> 

      <Data>Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Web.Services.Protocols.LogicalMethodInfo.Invoke(Object target, Object[] values) at Microsoft.Crm.Extensibility.InternalOperationPlugin.Execute(IServiceProvider serviceProvider) at Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext context) at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context) Inner Exception: System.InvalidCastException: Specified cast is not valid. at Microsoft.Crm.ObjectModel.TrackingManager.GetNextTrackingToken(String subject, String& trackingToken) at Microsoft.Crm.ObjectModel.EmailService.Send(Guid emailId, Boolean issueSend, String trackingToken, ExecutionContext context)</Data> 

      </EventData>

      </Event>

     

    ... still checking the CRM Trace log - it is quite verbose...

    Wednesday, June 22, 2011 1:44 PM
  • CRM Trace log is a tad more useful - it tells me:

    >Specified Cast is not valid

       at Microsoft.Crm.ObjectModel.TrackingManager.GetNextTrackingToken(String subject, String& trackingToken)

       at Microsoft.Crm.ObjectModel.EmailService.Send(Guid emailId, Boolean issueSend, String trackingToken, ExecutionContext context)

    >System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #0B11D52C: System.InvalidCastException: Specified cast is not valid.

    >exit: ConvertMessageAndErrorCode: errorCode: 0x80040216; message: System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #0B11D52C

    >Web Service Plug-in failed in SdkMessageProcessingStepId: {5E32C5E3-B29C-E011-ADA0-00155DC9837E}; EntityName: opportunity; Stage: 40; MessageName: Update; AssemblyName: Netcompany.CRM.Konica.Plugins.Opportunities.SendAdvisEmail, Netcompany.CRM.Konica.Plugins, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d5b3a98e2fd3a4a1; ClassName: Netcompany.CRM.Konica.Plugins.Opportunities.SendAdvisEmail; Exception: Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.

    >Crm Exception: Message: Object reference not set to an instance of an object., ErrorCode: -2147220956, InnerException: System.NullReferenceException: Object reference not set to an instance of an object.

     

    So obviously I must have made a mistake somewhere in my code, since I assume this wouldn't be the case for sample code. I've included my code below:

        protected void SendEmail(IOrganizationService service, Guid serviceChef, Guid opp, ITracingService trace)
        {
          try
          {
            ActivityParty fromParty = new ActivityParty
            {
              PartyId = new EntityReference(SystemUser.EntityLogicalName, opp)
            };
    
            ActivityParty toParty = new ActivityParty
            {
              PartyId = new EntityReference(SystemUser.EntityLogicalName, serviceChef)
            };
    
            Email email = new Email
            {
              To = new ActivityParty[] { toParty },
              From = new ActivityParty[] { fromParty },
              RegardingObjectId = new EntityReference(Opportunity.EntityLogicalName, opp),          
              Subject = "Support"
              Description = "Support",
              DirectionCode = true,
            };
    
            Guid _emailId = service.Create(email);
            
            SendEmailRequest sendEmailreq = new SendEmailRequest
            {
              EmailId = _emailId,
              TrackingToken = "",
              IssueSend = true
            };
    
            SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);
            trace.Trace("Mail sent");
          }
    
          catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
          {
            throw new InvalidPluginExecutionException(ex.InnerException.ToString() + " " + ex.InnerException.Message);
          }
    }
    


    Wednesday, June 22, 2011 2:13 PM
  • Please step in your plug in code. While debuggin try to find out where the is getting broken for below error.

    System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #0B11D52C: System.InvalidCastException: Specified cast is not valid

    It is invalid cast error. I suspect on below statements of code

    To = new ActivityParty[] { toParty },
    From = new ActivityParty[] { fromParty },
    RegardingObjectId = new EntityReference(Opportunity.EntityLogicalName, opp),

    OR

    Guid _emailId = service.Create(email);

    Please debug your plugin and find the culprit.


    Thanks, Ankit Shah
    Inkey Solutions, India.
    Microsoft Certified Business Management Solutions Professionals
    http://www.inkeysolutions.com/MicrosoftDynamicsCRM.html
    Thursday, June 23, 2011 5:46 AM
  • The odd thing is I've run the same bit of code from a console app - which worked perfectly. Must be the plugin-context that's off. Odd!
    Thursday, June 23, 2011 8:03 AM