2013-12-20 34 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; 
    } 
}