2013-12-20 40 views
0

我想在特定字段正在更新時觸發插件。CRM插件調用字段更新調用WCF

這裏是我打電話

public class CaseStageUpdate : IPlugin 
{ 
    public void Execute(IServiceProvider serviceProvider) 
    { 
     try 
     { 
      // Extract the tracing service for use in debugging sandboxed plug-ins. 
      ITracingService tracingService = 
       (ITracingService)serviceProvider.GetService(typeof(ITracingService)); 

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

      IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 

      Entity entity; 
      if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) 
      { 
       string strParams = "None"; 
       entity = (Entity)context.InputParameters["Target"]; 
       if (entity.LogicalName == "entityname") 
       { 
        // Create Matter Process 
        try 
        { 

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

         try 
         { 

          if (entity.Attributes.ContainsKey("field")){// call web service} 

         } 
         catch (Exception ex) 
         { 
          tracingService.Trace("Plugin Exception : \n {0}", ex.ToString()); 
          throw; 
         } 

        } 
        catch (Exception ex) 
        { 
         tracingService.Trace("Plugin Exception : \n {0}", ex.ToString()); 
         throw; 
        } 
       } 
      } 
      else 
      { 
       throw new InvalidPluginExecutionException("Plugin is not valid for this enity."); 
      } 
     } 
     catch (Exception ex) 
     { 

     } 
    } 
} 

在我更新實體WCF的代碼,但插件再次呼籲,因爲更新的領域在插件再次發現。但我沒有更新該領域。

假設我想更新AAA字段更新的ABC字段。我觸發設置AAA字段時,我會更新ABC字段。

WCF代碼

Entity entity = service.Retrieve("entityname", Guid.Parse(Id), new ColumnSet(true)); 
entity.Attributes["ABC"] = "TEST"; 
service.Update(entity); 
+0

重新閱讀您的問題後,我對您的實際問題有點困惑。你是否試圖從WCF服務中更新實體,並導致插件再次啓動? – Daryl

回答

1

should be using the Target對現有的實體更新現有的領域,沒有檢索到更新領域的實體。

// The InputParameters collection contains all the data passed in the message request. 
if (context.InputParameters.Contains("Target") && 
    context.InputParameters["Target"] is Entity) 
{ 
    // Obtain the target entity from the input parameters. 
    var entity = (Entity)context.InputParameters["Target"]; 
    entity.Attributes["ABC"] = "TEST"; 
    // No Need to call Update on the target. It'll get updated as a part of the Plugin Process 
} 

此外,刪除所有,但一個集你嘗試的的:

public void Execute(IServiceProvider serviceProvider) 
{ 
    try 
    { 
     // Extract the tracing service for use in debugging sandboxed plug-ins. 
     ITracingService tracingService = 
      (ITracingService)serviceProvider.GetService(typeof(ITracingService)); 

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

     IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 

     Entity entity; 
     if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity)) 
     { 
      throw new InvalidPluginExecutionException("Plugin is not valid for this enity."); 
     } 

     entity = (Entity)context.InputParameters["Target"]; 
     if (entity.LogicalName == "entityname") 
     { 
      // Create Matter Process 
      IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
      IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); 

      if (entity.Attributes.ContainsKey("field")){// call web service} 
     } 
    } 
    catch (Exception ex) 
    { 
     tracingService.Trace("Plugin Exception : \n {0}", ex.ToString()); 
     throw; 
    } 
}