简体   繁体   English

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

[英]How to update an Entity in CRM with a related Entity using a plug-in in CRM Dynamics 2013/2015

I am new to CRM and I am battling to update an entity using a Plugin, I have registered a plugin on the PhoneCall Entity pre-operation on create message, when I then query the regarding entity and write data from the regarding entity to my PhoneCall Entity, although I seem to get an error that the record with GUID xxxxx does not exist. 我是CRM新手,我正在努力使用插件来更新实体,我在创建消息时在PhoneCall Entity术前注册了一个插件,然后查询相关实体并将相关实体的数据写入我的PhoneCall实体,尽管我似乎收到一个错误,指出GUID xxxxx的记录不存在。

Here is my code below; 这是下面的代码;

  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");
                        }
                    }
                }


            }

The code fails on the service.Update(phoneCallEntity) ; 代码在service.Update(phoneCallEntity)上失败;

Your help will be appreciated. 您的帮助将不胜感激。

The problem is that you run your plugin in the pre-operation stage of the Create event. 问题是您在Create事件的操作前阶段运行插件。 In deed is the phonecall not created in this stage (and has no Id) and you get the reported error message. 实际上,在此阶段未创建电话呼叫(并且没有ID),您会收到报告的错误消息。 Actually you don´t have to update the phonecall by yourself using the service.Update method. 实际上,您不必使用service.Update方法自己更新电话。 Since the operation runs through you can just update the fields you like on the Target and let the CRM do the rest :) 由于操作一直在进行,因此您只需更新目标服务器上所需的字段,然后让CRM来完成其余的工作即可:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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