我有这个代码在创建记录的实时工作流程中运行。它基本上决定了它在哪个实体上运行,试图获得与字段相关的",它可以从实体变为另一个,然后查看相关记录是否是一个帐户,如果是,更新一些帐户的属性。
public override void ExecuteWorkflowLogic(XrmObjects xrmObjects)
{
using (XrmServiceContext ctx = new XrmServiceContext(xrmObjects.Service))
{
var target = xrmObjects.WorkflowContext.InputParameters["Target"] as Entity;
Xrm.Account account = null;
var regardingFieldName = string.Empty;
var logicalName = string.Empty;
var primaryKeyName = string.Empty;
switch (target.LogicalName)
{
case Xrm.Task.EntityLogicalName:
case Xrm.Email.EntityLogicalName:
case Xrm.PhoneCall.EntityLogicalName:
case Xrm.Letter.EntityLogicalName:
case Xrm.Appointment.EntityLogicalName:
case Xrm.Fax.EntityLogicalName:
regardingFieldName = "regardingobjectid";
logicalName = Xrm.ActivityPointer.EntityLogicalName;
primaryKeyName = "activityid";
break;
case Xrm.Annotation.EntityLogicalName:
logicalName = Xrm.Annotation.EntityLogicalName;
regardingFieldName = "objectid";
primaryKeyName = "annotationid";
break;
case Xrm.Opportunity.EntityLogicalName:
regardingFieldName = "customerid";
logicalName = Xrm.Opportunity.EntityLogicalName;
primaryKeyName = "opportunityid";
break;
case Xrm.Post.EntityLogicalName:
regardingFieldName = "regardingobjectid";
logicalName = Xrm.Post.EntityLogicalName;
primaryKeyName = "postid";
break;
}
var activity = (from a in ctx.CreateQuery(logicalName)
where a[regardingFieldName] != null && (Guid)a[primaryKeyName] == target.Id
select a).FirstOrDefault();
var ec = (EntityReference)activity[regardingFieldName];
if (ec == null)
return;
//if regarding isn't an account...
if (ec.LogicalName != Xrm.Account.EntityLogicalName)
return;
account = ctx.AccountSet.SingleOrDefault(x => x.Id == ec.Id);
if (account == null)
return;
account.new_last_activity_created_by = (EntityReference)target["createdby"];
account.new_last_activity_date = (DateTime)target["createdon"];
account.new_last_activity_type = GetActivityType(target); //returns an optionset based on entity logical name....
account.new_last_activity_entity_logicalname = target.LogicalName;
if (!ctx.IsAttached(account))
ctx.Attach(account);
ctx.UpdateObject(account);
ctx.SaveChanges();
}
}
我希望它在实时工作流程中运行,因为更改将是即时的,因为用户创建上面的实体可能无权更新帐户,因此实时工作流程允许我以具有相应权限的其他用户(工作流所有者)进行更新。
问题在于它是一个活动实体(switch语句中的第一个大块),记录看起来好像没有提交到数据库,而且我没办法获取它并进一步查看它(activity
结束为null)
如果我在target
或ActivityPointerBase
,TaskBase
等中查找EmailBase
的GUID,我在执行此操作时并未在任何地方看到它工作流程(在我创建的记录创建后发生)
如果我在异步工作流程中运行它,它适用于所有实体。
在实时模式中,它仅适用于机会,帖子和注释。我正在测试中从社交Feed菜单创建活动。
上面的代码在扩展BaseWorkflowStep
的类的方法中运行:
public abstract class BaseWorkflowStep : CodeActivity
{
protected override void Execute(CodeActivityContext context)
{
var xrmObjects = new XrmObjects();
var serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
xrmObjects.CodeActivityContext = context;
xrmObjects.TracingService = context.GetExtension<ITracingService>();
xrmObjects.WorkflowContext = context.GetExtension<IWorkflowContext>();
xrmObjects.Service = serviceFactory.CreateOrganizationService(xrmObjects.WorkflowContext.UserId);
ExecuteWorkflowLogic(xrmObjects);
}
public virtual void ExecuteWorkflowLogic(XrmObjects xrmObjects)
{
throw new NotImplementedException();
}
}
这里发生了什么?