我結束了
- 更改關係指涉
- 並稱,在變化的子實體查找到它的執行異步工作流的父
- 查找是強制性
- 的工作流程檢查字段是否爲空,並且如果它是
這個自定義工作流活動刪除當前記錄:
public class DeleteCurrentRecord : CodeActivity
{
protected override void Execute(CodeActivityContext context)
{
if (context == null)
{
throw new InvalidPluginExecutionException("Workflow context error. Please retry the operation.");
}
using (var workflowContext = new WorkflowContext(context))
{
DeleteRecord(workflowContext, context);
}
}
public void DeleteRecord(WorkflowContext context, CodeActivityContext activityContext)
{
var targetId = context.Target != null ? context.Target.Id : context.TargetReference.Id;
var targetName = context.Target != null ? context.Target.LogicalName : context.TargetReference.LogicalName;
context.Service.Delete(targetName, targetId);
}
}
這是我寫的那得到的一切我從CodeActivityContext需要的輔助類。
它只是節省了爲每個工作流程編寫相同的鍋爐板代碼。
我也可以構造一個對象在控制檯應用程序在本地測試工作流程:
/* WorkflowContext is a helper class that I wrote that gets all the CRM services
* and everything else from a CodeActivityContext
* I then pass an instance of it to methods
* I add properties to WorkflowContext as needed
* */
/* Contains objects that are taken from CodeActivityContext
* */
public class WorkflowContext : IDisposable
{
public IOrganizationService Service { get; private set; }
public Entity Target { get; private set; }
public EntityReference TargetReference { get; private set; }
public Context Linq { get; private set; }
public IWorkflowContext CurrentContext { get; set; }
public string PrimaryEntityName { get; set; }
public int Depth { get; set; }
public WorkflowContext(CodeActivityContext activityContext)
{
IWorkflowContext context = activityContext.GetExtension<IWorkflowContext>();
CurrentContext = context;
PrimaryEntityName = context.PrimaryEntityName;
Depth = context.Depth;
IOrganizationServiceFactory factory = activityContext.GetExtension<IOrganizationServiceFactory>();
if (context.InputParameters.Contains("Target")) // On demand workflows do not have target
{
if (context.InputParameters["Target"].GetType() == typeof(Entity))
Target = (Entity)context.InputParameters["Target"];
else if (context.InputParameters["Target"].GetType() == typeof(EntityReference)) // delete and some other operations are EntityReference
TargetReference = (EntityReference)context.InputParameters["Target"];
}
Service = factory.CreateOrganizationService(context.InitiatingUserId);
Linq = new Context(Service);
}
// Constructor for testing
public WorkflowContext(IOrganizationService service, Context linq, EntityReference targetReference, Entity target, bool inTransaction)
{
this.Service = service;
this.Linq = linq;
this.TargetReference = targetReference;
this.Target = target;
}
public void Dispose()
{
Linq.Dispose();
}
}
謝謝你的答案。我試了一下,發現ParentContext對於父實體的刪除和級聯刪除都是空的。你能指點我其他方向嗎? – Bvrce
在這種情況下,我會建議使用異步插件而不是工作流。 –