Почему CRM 2011 Entity Relationship не имеет значения в этом коде плагина?

Это рабочий пример плагина, который я написал для CRM 2011. Я создал шаг «Создать» в инструменте регистрации плагина для этого плагина. Это хорошо работает. У меня также есть шаг «Обновление», зарегистрированный для плагина. Это не может быть выполнено, поскольку основной возвращенный контакт имеет значение null. Оба этих шага зарегистрированы как асинхронные.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using System.IO;
using System.Net;

namespace CRMNewsletterPlugin
{
    public class NewsletterSignup : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
            serviceProvider.GetService(typeof(IPluginExecutionContext));

            tracingService.Trace("Begin load");
            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
            {
                tracingService.Trace("We have a target.");
                // Obtain the target entity from the input parmameters.
                Entity entity = (Entity)context.InputParameters["Target"];
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                if (!entity.GetAttributeValue<bool>("new_receivesnewsletter"))
                {
                    try
                    {
                        //check if the account number exist
                        string emailAddress = entity.GetAttributeValue<string>("emailaddress1");
                        EntityReference primaryContact = entity.GetAttributeValue<EntityReference>("primarycontactid");

                        // UPDATE STEP FAILS HERE
                        if (primaryContact == null)
                        {
                            tracingService.Trace("Primary Contact is null");
                        }
                        Entity contact = service.Retrieve("contact", primaryContact.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(new string[] { "fullname" }));
                        string fullname = contact.GetAttributeValue<string>("fullname");
                        string name = entity.GetAttributeValue<string>("name");
                        WebRequest req = WebRequest.Create("http://localhost");
                        string postData = "cm-name=" + fullname + "&cm-ddurhy-ddurhy=" + emailAddress + "&cm-f-jddkju=" + name;
                        tracingService.Trace(postData);
                        byte[] send = Encoding.Default.GetBytes(postData);
                        req.Method = "POST";
                        req.ContentType = "application/x-www-form-urlencoded";
                        req.ContentLength = send.Length;
                        tracingService.Trace("Sending info");

                        Stream sout = req.GetRequestStream();
                        sout.Write(send, 0, send.Length);
                        sout.Flush();
                        sout.Close();
                        tracingService.Trace("Info sent");
                        WebResponse res = req.GetResponse();
                        StreamReader sr = new StreamReader(res.GetResponseStream());
                        string returnvalue = sr.ReadToEnd();
                        tracingService.Trace(returnvalue);

                        entity.Attributes["new_receivesnewsletter"] = true;
                        service.Update(entity);
                        tracingService.Trace("Newsletter field set");

                    }
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                    }

                }
            }

        }
    }//end class
}

Ответы на вопрос(1)

Ваш ответ на вопрос