c# - 如何使用 CRM Dynamics 2013/2015 中的插件更新 CRM 中的实体和相关实体

标签 c# plugins dynamics-crm-2015

我是 CRM 的新手,我正在努力使用插件更新实体,我已在创建消息时的 PhoneCall 实体预操作上注册了一个插件,然后,当我查询相关实体并将数据从相关实体写入我的 PhoneCall 实体时,尽管我似乎收到一条错误,表明 GUID xxxxx 的记录不存在。

下面是我的代码;

  public void Execute(IServiceProvider serviceProvider)
    {
        ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
        IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        if (context == null)
        {
            throw new ArgumentNullException("localContext");
        }

        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

        IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);

        if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
        {
            Entity phoneCallEntity = (Entity)context.InputParameters["Target"];

            if (phoneCallEntity.LogicalName != "phonecall")
                return;


            if (context.MessageName == "Create")
            {
                try
                {
                    QueryExpression qe = new QueryExpression("new_zoiperhistorydata");
                    qe.ColumnSet = new ColumnSet("new_regardingobjectid", "new_callduration");

                    var ZoiperData = service.RetrieveMultiple(qe);

                    if (ZoiperData != null && ZoiperData.Entities.Count > 0)
                    {

                        foreach (Entity entity in ZoiperData.Entities)
                        {
                            if (entity.Attributes.Contains("new_callduration"))
                                phoneCallEntity.Attributes["new_callduration"] = entity.Attributes["new_callduration"].ToString();
                        }
                    }

                    service.Update(phoneCallEntity);
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine("The application terminated with an error.");
                    Console.WriteLine(ex.Message);

                    // Display the details of the inner exception.
                    if (ex.InnerException != null)
                    {
                        Console.WriteLine(ex.InnerException.Message);

                        FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
                            as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>;
                        if (fe != null)
                        {
                            Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
                            Console.WriteLine("Code: {0}", fe.Detail.ErrorCode);
                            Console.WriteLine("Message: {0}", fe.Detail.Message);
                            Console.WriteLine("Trace: {0}", fe.Detail.TraceText);
                            Console.WriteLine("Inner Fault: {0}",
                                null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
                        }
                    }
                }


            }

代码在 service.Update(phoneCallEntity) 上失败;

我们将不胜感激您的帮助。

最佳答案

问题在于您在创建事件的预操作阶段运行插件。实际上,电话调用未在此阶段创建(并且没有 ID),并且您会收到报告的错误消息。 实际上,您不必使用 service.Update 方法自己更新电话。由于操作贯穿始终,您只需在 Target 上更新您喜欢的字段,然后让 CRM 完成其余的工作:)

关于c# - 如何使用 CRM Dynamics 2013/2015 中的插件更新 CRM 中的实体和相关实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32607161/

相关文章:

c# - .net 框架是否支持在不同版本 1.1 和 3.5 之间使用策略文件进行程序集重定向?

dynamics-crm-2015 - 如何在 Dynamic CRM 中重命名沙盒文本

javascript - 从联系人查找中查看联系人信息

wordpress - 从外部插件编辑 wp-config.php

java - 如何在 Java 中共享类的接口(interface)而不泄露整个源代码(如 .h 文件)

dynamics-crm-2011 - 重命名 Dynamics CRM 组织名称

c# - 四舍五入

C# 正则表达式匹配列表中的单词

c# - BitmapImage:访问关闭的 StreamSource

plugins - 没有注释时如何为函数生成JSDoc注释?