【问题标题】:Why is CRM 2011 Entity Relationship null in this plugin code?为什么此插件代码中的 CRM 2011 实体关系为空?
【发布时间】:2011-10-02 23:05:15
【问题描述】:

这是我为 CRM 2011 编写的插件的一个工作示例。我在插件注册工具中为此插件创建了一个“创建”步骤。这执行得很好。我还为插件注册了一个“更新”步骤。这无法执行,因为返回的主要联系人为空。这些步骤都注册为异步的。

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
}

【问题讨论】:

    标签: .net plugins dynamics-crm-2011


    【解决方案1】:

    InputParameters["Target"] 中包含的实体仅包括在更新中提交的更改字段,而不是所有字段。您的插件适用于 Create,因为 InputParameters["Target"] 显然在 Create 期间始终包含所有字段。

    要解决此问题,您需要在包含 primarycontactid 字段的 PluginRegistrationTool 步骤中添加 PreImage。 PreImage 将始终包含您指定的字段的值更新前的值

    诀窍是首先检查 InputParameters["Target"] 上的 primarycontactid,因为这将包含最新值(例如,如果用户在同一保存期间更新了 new_receivesnewsletter 和 primarycontactid)。如果 InputParameters["Target"] 不包含 primarycontactid,则返回您的 PreImage,访问类似这样的内容(假设您将 PreImage 的别名设置为“preimage”):

    context.PreEntityImages["preimage"].GetAttributeValue<EntityReference>("primarycontactid")
    

    希望有帮助!

    【讨论】:

    • 你这家伙乔希!谢谢(你的)信息。奇怪的是,我将我的原像命名为“account_preimage”,但集合中的键是“帐户别名”,即“帐户”。
    • 有趣!我通常对名称和别名都使用“原像”,所以很高兴知道别名是实际的魔术键。我会更新我的答案。谢谢!
    • 刚刚花了一周时间试图弄清楚为什么我的插件在更新时不起作用,但在创建时起作用 - 您在 3 段中回答了它!干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2012-01-09
    • 2012-04-18
    • 2014-04-29
    • 1970-01-01
    • 2013-01-19
    相关资源
    最近更新 更多