简体   繁体   中英

c# - CRM 2011 Create a record when an email is received

I want to create a plug in that will create a record based on the specific format that can be found on the email body. For example:

PO/Dustine/Tolete/8:45 PM/Sample Location/sample desc

So far, I have this code:

using System;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Xrm;

public class Plugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)
        serviceProvider.GetService(typeof(IPluginExecutionContext));

        Entity entity;

        // Check if the input parameters property bag contains a target
        // of the create operation and that target is of type Entity.
        if (context.InputParameters.Contains("Target") &&
        context.InputParameters["Target"] is Entity)
        {
            // Obtain the target business entity from the input parameters.
            entity = (Entity)context.InputParameters["Target"];

            // Verify that the entity represents a contact.
            if (entity.LogicalName != "email") { return; }
        }
        else
        {
            return;
        }

        try
        {
            IOrganizationServiceFactory serviceFactory =
                (IOrganizationServiceFactory)serviceProvider.GetService(
            typeof(IOrganizationServiceFactory));
            IOrganizationService service =
            serviceFactory.CreateOrganizationService(context.UserId);

            var id = (Guid)context.OutputParameters["id"];

            AddNewServiceRequest(service, id);
        }
        catch (FaultException<OrganizationServiceFault> ex)
        {
            throw new InvalidPluginExecutionException(
            "An error occurred in the plug-in.", ex);
        }
    }

    private static void AddNewServiceRequest(IOrganizationService service, Guid id)
    {
        using (var crm = new XrmServiceContext(service))
        {

            var email = crm.EmailSet.Where(c => c.ActivityId == id).First();

            string[] noteText = email.Description.ToString().Split('/');

            foreach(string text in noteText){

                Console.WriteLine(text);
            }

            Entity peaceAndOrder = new Entity("msa_noisecomplaintrequest");

            peaceAndOrder["msa_firstname"] = noteText[1];
            peaceAndOrder["msa_lastname"] = noteText[2];
            peaceAndOrder["msa_incidenttime"] = noteText[3];
            peaceAndOrder["msa_location"] = noteText[4];
            peaceAndOrder["msa_description"] = noteText[5];

            service.Create(peaceAndOrder);
        }
    }
}

But everytime the event is triggered, an error is occurring. What am I doing wrong?

You're using context.OutputParameters to get the email Id. I assume that your plug-in is registered on a post create event. Ensure that step is indeed registered properly (ie running on the post event) and that there are no other plug-in running on the email entity.

If you do have other plug-ins running on email (such as a pre event plug-in) you must wrap your code with a proper condition ensuring this only runs on post create event ie

if (context.Stage == 40 /*Post Operation*/) 
{
  //  Your code here …
}

It's also good practice to wrap your code with a condition checking the correct message name ie

if (context.MessageName == "CREATE")
{
   //  Your code here …
}

As a side note, you defined AddNewServiceRequest as static. plug-ins are Cached by CRM so they are static in a sense. You don't need to declare static member in your code unless you intend to share data between plug-ins or when declaring static members inside objects.

And finally, enable platform tracing or debug to get a glimpse of what really causing this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM