Skip to main content
 首页 » 编程设计

c#之通过工作流更新字段,更好的方法

2024年02月05日18bhlsheji

我被要求创建一个 View ,其中包含适合某个日期范围的实体。因此,如果实体的 new_date1 字段小于今天,而其 new_date2 字段大于今天,则实体应该出现在表单的子网格中。

不幸的是,您不能使用简单的 View 执行此操作,因为 FetchXML 不支持可能返回今天日期的计算和运算符。

我想出了在实体上创建一个 Active 字段的想法,然后让 javascript 规则根据输入的日期范围设置该字段。

然后 View 可以使用 Active 字段作为过滤条件。

问题是,如果实体的表单在一段时间内没有打开,实体可能会变得不活动(例如,今天的日期现在已经超过了 date1 和 date2)但如果用户没有打开实体的表单,该字段将不会自行更新,并且 View 会将非事件实体显示为事件实体。

所以我想有一个计划的工作流收集所有应该活跃或不活跃的实体,然后这个工作流启动一个子工作流,将 Active 标志设置为是或否。

这里是一些涉及的代码:

private void LaunchUpdateOpportunityWorkflow(IOrganizationService service, ITracingService tracingService, DataCollection<Entity> collection, bool active) 
{ 
 
        foreach (Entity entity in collection) 
        { 
            //launch a different workflow, depending on whether we want it active or inactive... 
            Guid wfId = (active) ? setActiveWorkflowId : setInactiveWorkflowId; 
            ExecuteWorkflowRequest execRequest = new ExecuteWorkflowRequest(); 
            execRequest.WorkflowId = wfId; 
            execRequest.EntityId = (Guid)entity["opportunityid"]; 
 
            try 
            { 
                CrmServiceExtensions.ExecuteWithRetry<ExecuteWorkflowResponse>(service, execRequest); 
            } 
            catch (Exception ex) 
            { 
                tracingService.Trace(string.Format("Error executing workflow for opportunity {0}: {1}", entity["opportunityid"], ex.Message)); 
            } 
        } 
 
} 

收集相关DataCollection 的过程是通过简单的RetrieveMultipleRequest 请求完成的。

该方法的问题在于,如果服务器重新启动,则必须有人去启动运行上述代码的工作流。

有更好的方法吗?我正在使用 MS CRM 2016。

请您参考如下方法:

添加到 Jame 的答案中,如果过滤条件变得复杂而无法使用 fetchxml 实现,您始终可以使用插件。

在“RetrieveMultiple”消息上注册一个插件。

var queryExpression = PluginExecutionContext.InputParameters["Query"]; 
if(queryExpression == null || !queryExpression.EntityName.equals("yourentityname", StringComparison.InvariantCultureIgnoreCase) return; 

添加一个高级查找独有的条件,因为无法过滤出哪个高级查找触发了您实体上的插件,实现此目的的最简单方法是添加一个属性并在高级查找查询。

检查条件,如果找到,则用户正在尝试运行您设置的高级查找:

if (queryExpression.Criteria == null || queryExpression.Criteria.Conditions == null || 
!queryExpression.Criteria.Conditions.Any()) return; 

找到匹配的条件,这样你就可以删除它并添加你想要过滤数据的条件:

 var matchContidion = queryExpression.Criteria.Conditions.FirstOrDefault(c => c.AttributeName == "yourflagattribute"); 
 if (matchContidion == null) return; 

删除虚拟匹配条件并添加您自己的条件:

queryExpression.Criteria.Conditions.Remove(matchContidion); 
queryExpression.Criteria.Conditions.Add(new ConditionExpression("new_date1", ConditionOperator.LessThan, DateTime.Now)); 
queryExpression.Criteria.Conditions.Add(new ConditionExpression("new_field2", ConditionOperator.Equals, "Some complex value which cannot be set using fetchxml")); //for example, based on certain values, you might want to call a webservice to get the filter value.