为什么CRM 2011实体关系无效在这个插件code? [英] Why is CRM 2011 Entity Relationship null in this plugin code?

查看:224
本文介绍了为什么CRM 2011实体关系无效在这个插件code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是为CRM 2011,我创建的插件注册工具为这个插件的一个创建的步骤,我已经写了一个插件的工作示例。这执行罚款。我也注册了一个插件'更新'的一步。这种无法执行,因为返回的主要联系人为空。这些步骤都注册为异步的。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用Microsoft.Xrm.Sdk;
使用System.ServiceModel;
使用System.IO;
使用System.Net;

命名空间CRMNewsletterPlugin
{
    公共类NewsletterSignup:IPlugin
    {
        公共无效执行(的IServiceProvider的ServiceProvider)
        {
            ITracingService tracingService =(ITracingService)serviceProvider.GetService(typeof运算(ITracingService));
            //获取来自服务提供商的执行上下文。
            IPluginExecutionContext上下文=(IPluginExecutionContext)
            serviceProvider.GetService(typeof运算(IPluginExecutionContext));

            tracingService.Trace(开始加载);
            //该InputParameters集合包含了所有的消息请求传递的数据。
            如果(context.InputParameters.Contains(目标)及和放大器;
            context.InputParameters [目标]是实体)
            {
                tracingService.Trace(我们有一个目标。);
                //获取从输入parmameters目标实体。
                实体实体=(实体)context.InputParameters [目标];
                IOrganizationServiceFactory服务工厂=(IOrganizationServiceFactory)serviceProvider.GetService(typeof运算(IOrganizationServiceFactory));
                IOrganizationService服务= serviceFactory.CreateOrganizationService(context.UserId);
                如果(entity.GetAttributeValue&所述;!布尔&GT(new_receivesnewsletter))
                {
                    尝试
                    {
                        //检查账号存在
                        字符串EMAILADDRESS = entity.GetAttributeValue<字符串>(emailaddress1);
                        的EntityReference primaryContact = entity.GetAttributeValue&其中;的EntityReference&GT(primarycontactid);

                        //更新步骤失败HERE
                        如果(primaryContact == NULL)
                        {
                            tracingService.Trace(主要联系人为空);
                        }
                        实体接触= service.Retrieve(接触,primaryContact.Id,新Microsoft.Xrm.Sdk.Query.ColumnSet(新的String [] {全名}));
                        字符串全称= contact.GetAttributeValue<字符串>(全名);
                        字符串名称= entity.GetAttributeValue<字符串>(姓名);
                        WebRequest的REQ = WebRequest.Create(HTTP:// localhost的);
                        字符串POSTDATA =CM-NAME =+真实姓名+&放大器; CM-ddurhy-ddurhy =+ EMAILADDRESS +&放大器; CM-F-jddkju =+名;
                        tracingService.Trace(POSTDATA);
                        byte []的发送= Encoding.Default.GetBytes(POSTDATA);
                        req.Method =POST;
                        req.Co​​ntentType =应用/的X WWW的形式urlen codeD;
                        req.Co​​ntentLength = send.Length;
                        tracingService.Trace(发送信息);

                        流Sout = req.GetRequestStream();
                        sout.Write(发送,0,send.Length);
                        sout.Flush();
                        sout.Close();
                        tracingService.Trace(信息发送);
                        WebResponse的解析度= req.GetResponse();
                        StreamReader的SR =新的StreamReader(res.GetResponseStream());
                        字符串返回值= sr.ReadToEnd();
                        tracingService.Trace(返回值);

                        entity.Attributes [new_receivesnewsletter] =真;
                        service.Update(实体);
                        tracingService.Trace(通讯领域集);

                    }
                    赶上(FaultException异常前)
                    {
                        抛出新InvalidPluginExecutionException(在插件中发生错误。前);
                    }

                }
            }

        }
    } //结束类
}
 

解决方案

中包含的InputParameters实体[目标]只包含的提交的更新的是,改田,不是所有领域。你的插件适用于创建因为InputParameters [目标]始终包含在一个所有字段创建效果显着。

要解决这个问题,你需要添加一个preImage在PluginRegistrationTool的步骤,其中包括primarycontactid领域。一个preImage将始终包含字段的值指定的,因为他们在更新之前的。

诀窍是[目标]首先检查primarycontactid上InputParameters,因为那样会包含最新值(如果用户在同一个更新的两个new_receivesnewsletter和primarycontactid保存,例如)。如果InputParameters [目标]不包含primarycontactid,然后退回到您的preImage,访问是这样的(假设你设置你的preImage的别名为preimage):

<$p$p><$c$c>context.$p$pEntityImages["$p$pimage"].GetAttributeValue<EntityReference>("primarycontactid")

希望帮助!

this is a working example of a plugin that I have written for CRM 2011. I have created a 'Create' step in the plugin registration tool for this plugin. This executes fine. I also have an 'Update' step registered for the plugin. This fails to execute because the primary contact returned is null. These steps are both registered as asynchronous.

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
}

解决方案

The entity contained in the InputParameters["Target"] only includes the changed fields that were submitted in the update, not all fields. Your plugin works for Create because InputParameters["Target"] always contains all fields during a Create obviously.

To fix this, you need to add a PreImage to your step in PluginRegistrationTool that includes the primarycontactid field. A PreImage will always include the values for the fields you specify as they were before the update.

The trick is to check for primarycontactid on InputParameters["Target"] first, because that would contain the latest value (if the user updated both new_receivesnewsletter and primarycontactid during the same save, for example). If InputParameters["Target"] doesn't contain primarycontactid, then fall back on your PreImage, accessed something like this (assuming you set your PreImage's alias to "preimage"):

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

Hope that helps!

这篇关于为什么CRM 2011实体关系无效在这个插件code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆