回答

1

您可以使用Plugin,而不是自定義工作流,並將其註冊的「檢索」的消息。

public void Execute(IServiceProvider serviceProvider) 
{ 
// Obtain the execution context from the service provider. 
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) 
    serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); 

if (context.Depth == 1) 
{ 
    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); 

    // Obtain the target entity from the input parmameters. 
    EntityReference entity = (EntityReference)context.InputParameters["Target"]; 

    ColumnSet cols = new ColumnSet(
         new String[] { "lastname", "firstname", "address1_name" }); 

    var contact = service.Retrieve("contact", entity.Id, cols); 

    if (contact != null) 
    { 
     if (contact.Attributes.Contains("address1_name") == false) 
     { 
      Random rndgen = new Random(); 
      contact.Attributes.Add("address1_name", "first time value: " + rndgen.Next().ToString()); 
     } 
     else 
     { 
      contact["address1_name"] = "i already exist"; 
     } 
     service.Update(contact); 
    } 
    } 
} 

enter image description here

CRM 2011–Retrieve Plugin

2

如果你想觸發自定義工作流活動,並且不需要做任何事情在它的工作流程相關的,我建議你創建一個自定義操作。它與工作流非常相似,但CRM將爲您創建一個自定義終點。它消除了跟蹤工作流ID的需要...