c# – 记录创建的实时工作流在记录提交到数据库之前运行
作者:互联网
我有这个代码在创建记录的实时工作流程中运行.它基本上决定了它在哪个实体上运行,尝试获取“相关字段”,可以从实体更改为另一个,然后查看相关记录是否为帐户,如果是,则更新部分帐户的属性.
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语句中的第一个大块)时,记录看起来好像没有提交到数据库,并且我无法获取它并进一步查看它(活动结束了空值)
如果我在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();
}
}
这里发生了什么事 ?
解决方法:
这可能现在已经太旧了,但是如果您使用的是实时工作流程(RTWF),那么当您的WF正在运行时,该记录将不会被提交.这部分是因为如果您的RTWF失败,则失败将取消创建主记录.因此,主记录和代码结果是同一个数据库事务的一部分.这也是异步工作的原因,因为这些操作发生在单独的数据库事务中.
而不是查询数据,而是使用在Context中传递的图像.例如,如果在创建记录后发生这种情况,则可以使用PostEntityImage集合中的“PostBusinessEntity”.
标签:c,net,dynamics-crm,dynamics-crm-2016 来源: https://codeday.me/bug/20190711/1430199.html