暂停CRM 2013工作流程5分钟

时间:2015-04-09 12:22:01

标签: dynamics-crm-2013

在我的工作流程中,我必须实现暂停5毫秒工作流程的逻辑。我写了下面的代码。

var query = new QueryExpression("asyncoperation");
query.ColumnSet.AllColumns = true;
query.Criteria = new FilterExpression();
query.Criteria.AddCondition(new ConditionExpression("name", ConditionOperator.Equal, "workflowname"));

var results = crmService.RetrieveMultiple(query);
var rerunworkflow = results.Entities.FirstOrDefault();

Entity operation = new Entity("asyncoperation") {  Id = rerunworkflow.GetAttributeValue<Guid>("asyncoperationid") };
operation["postponeuntil"] = DateTime.Now.AddMinutes(5);
operation["statecode"] = new OptionSetValue(1);
operation["statuscode"] = new OptionSetValue(10);
crmService.Update(operation);

但是,当此工作流程被激活并被调用时,它会向我显示以下错误。请求的状态转换对当前状态无效。当前状态:3,当前状态:30,目标状态:1。为什么会出现此问题?以前可能在crm 4.0中,现在它显示工作流程处于已完成状态。请指教一下。谢谢,Kandarp

1 个答案:

答案 0 :(得分:0)

请在此页面上看到答案: https://community.dynamics.com/crm/f/117/p/157381/372199.aspx#372199

 Entity dynamicEntity = GetAsyncOperationInfo(crmService, context.OperationId);
 // update the AsyncOperation as suspended (10) postponed of 5 minutes
 Entity asyncOperation = new Entity("asyncoperation");
 asyncOperation.Id = context.OperationId;
 asyncOperation["statecode"] = new OptionSetValue(10);
 asyncOperation["postponeuntil"] = DateTime.Now.AddMinutes(5);
 crmService.Update(asyncOperation);
 if (dynamicEntity != null && dynamicEntity.Contains("createdon"))
 {
   DateTime createdon = ((DateTime)dynamicEntity["createdon"]);
   TimeSpan span = DateTime.UtcNow - createdon; 
 // note the UtcNow instead the `Now, this because createdon from CRM comes as UTC
   if (span.Minutes < 30)
   {
       System.Threading.Thread.Sleep(new TimeSpan(0, 5, 0));
   }
}