簡體   English   中英

如何在Dynamics CRM 2011中更新時檢索實體

[英]how to retrieve entity on update in dynamics crm 2011

我是CRM的新手,正在嘗試編寫插件。

它應該很簡單,但事實並非如此。

由於某種原因,我無法檢索實體。 我肯定知道它存在於數據庫中並且ID是正確的。

下面的代碼。

有誰知道為什么它不起作用? 謝謝

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

namespace Copy_field
{
    public class Copy_field : IPlugin
    {
        /// <summary>
        /// A plugin copyies fields from Contact Entity to Case Entity. This allows display
        /// information about the client on case Entity and change it directly from Case Entity 
        /// </summary>
        /// <remarks>Register this plug-in on the Create case, update case and update of contact
        /// </remarks>
        /// 

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

            ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            Entity entity;

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

                entity = (Entity)context.InputParameters["Target"];
                if (entity.LogicalName != "incident") { return; } 
            }
            else
            {
                return;
            }
        //    tracer.Trace("1. THIS IS an/a: " + entity.LogicalName);
          //  tracer.Trace("2. Id: " + entity.Id);
   //         tracer.Trace("2.2 output parametersId: " + context.OutputParameters["id"].ToString());

            // if record exists - retrieve the entity

            ///////////////////////////////////////////////////////////////////
        ColumnSet cols = new ColumnSet(true);
        Entity yahhooo = service.Retrieve(entity.LogicalName, entity.Id, cols);


        }
    }
}

不要忘記,在update消息中,您只能訪問要對其進行更新(更改)的文件...您可以按以下方法編寫,以跟蹤您的代碼(行到行)

  public static void LogFile(string log)
    {
        try
        {
            TextWriter tw = new StreamWriter(@"C:\Inetpub\wwwroot\inventorylog_" + Guid.NewGuid() + ".txt");
            tw.Write(log);
            tw.Close();
        }
        catch
        {
        }
    }

成功的

好吧,您可以給我們更多有關它的信息,例如是否拋出異常? 我的猜測是,您收到以下錯誤:“ ID中為“ yourentityid”的“ yourentity”在數據庫中不存在”,如果是這樣,則您可能正在嘗試檢索在操作前管道上創建的記錄。 無論如何,請幫助我們幫助您提供更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM