业务流程字段控制
业务流程上可以添加绑定实体的字段,既有字段,那么就有字段的控制。设置业务流程字段的必填,可以在业务流程上勾选required即可。那么如何锁定业务流程上的字段呢,其实很简单,只需在字段名称前加上header_process_即可。那我们就可以使用getControl下所有的方法啦!
Xrm.Page.getControl("header_process_tec_currencyid").setDisabled(true)
业务流程阶段更改后触发事件
//窗体OnLoad中添加以下代码
//addOnStageChange() 为回调函数阶段更改后触发
Xrm.Page.data.process.addOnStageChange(onStageChange);
//声明onStageChange
//阶段改变事件
function onStageChange() {
//获取下一阶段名称
var stageName = Xrm.Page.data.process.getActiveStage().getName();
、、、、、、
、、、、、、
}
业务流程阶段更改前触发事件
更改前触发的事件只需在窗体OnLoad中添加方法,将上下文作为参数传入即可。
function AddOnPreStageChange(ExecutionContext) {
//update
if (Xrm.Page.ui.getFormType() == FORM_TYPE_UPDATE) {
var objFormContext = ExecutionContext.getFormContext();
objFormContext.data.process.addOnPreStageChange(function (ExecutionContext) {
var BPFArgs = ExecutionContext.getEventArgs();
//阶段向后推进
if (BPFArgs.getDirection() === "Next") {
var objFormContext = ExecutionContext.getFormContext();
//获取窗体上字段的值
var stage = objFormContext.getAttribute('tec_projectstage').getValue();
、、、、、、
业务逻辑
、、、、、、
}
//阶段向前推进
else if (BPFArgs.getDirection() === "Previous") {
//阻止推进
BPFArgs.preventDefault();
Xrm.Navigation.openAlertDialog('Attention: workflow can not move back to previous stage!');
}
});
}
}
业务流程后台自动推进
//获取对应业务流程
//获取当前项目对应业务流程
string DealProcStr = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
DealProcStr += "<entity name='" + businessProcessName + "'>";
DealProcStr += "<attribute name='processid' />";
DealProcStr += "<attribute name='businessprocessflowinstanceid' />";
DealProcStr += "<attribute name='bpf_" + getData.LogicalName + "id' />";
DealProcStr += "<filter type='and'>";
DealProcStr += "<condition attribute='bpf_" + getData.LogicalName + "id' operator='eq' value='" + getData.Id + "' />";
DealProcStr += "</filter>";
DealProcStr += "</entity>";
DealProcStr += "</fetch>";
DataCollection<Entity> Process = svr.GetEntityDataByFetchXml(DealProcStr);
if (Process.Count > 0)
{
//查找流程下一阶段id
//stagename 为你想要推进到哪一阶段的名称
string StageFetch = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
StageFetch += "<entity name='processstage'>";
StageFetch += "<attribute name='processstageid' />";
StageFetch += "<attribute name='processid' />";
StageFetch += "<attribute name='stagename' />";
StageFetch += "<filter type='and'>";
StageFetch += "<condition attribute='processid' operator='eq' value='" + ((EntityReference)entities[0]["processid"]).Id + "' />";
StageFetch += "<condition attribute='stagename' operator='eq' value='白银' />";
StageFetch += "</filter>";
StageFetch += "</entity>";
StageFetch += "</fetch>";
FetchExpression NextStegefetch = new FetchExpression(StageFetch);
DataCollection<Entity> NextStegeid = service.RetrieveMultiple(NextStegefetch).Entities;
if (NextStegeid.Count > 0)
{
if (NextStegeid[0].Contains("processstageid"))
{
Entity getProcess = new Entity("new_accountprocesses");
getProcess.Id = entities[0].Id;
getProcess["activestageid"] = new EntityReference("processstage", NextStegeid[0].Id);
//更新流程阶段
service.Update(getProcess);
}
}
}
更换业务流程
业务流程创建则添加了一个以业务流程为名称的实体,通过对实体记录的操作就可以更改业务流程了!
/// <summary>
/// 选择业务流程
/// </summary>
/// <param name="entityName">实体唯一名称</param>
/// <param name="entityId">实体ID</param>
/// <param name="businessProcessEntityName">更改后业务流程的唯一名称</param>
/// <param name="preBusProcessEntityNamee">将要替换的业务流程名称(允许为空)</param>
public void SwitchProcesses(string entityName, Guid entityId, string businessProcessEntityName, string preBusProcessEntityNamee = null)
{
//根据业务流程的唯一名称查找业务流程
string DealProcStr = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
DealProcStr += "<entity name='workflow'>";
DealProcStr += "<attribute name='uniquename' />";
DealProcStr += "<filter type='and'>";
DealProcStr += "<condition attribute='uniquename' operator='eq' value='"+ businessProcessEntityName + "' />";
DealProcStr += "</filter>";
DealProcStr += "</entity>";
DealProcStr += "</fetch>";
FetchExpression fetchxml = new FetchExpression(DealProcStr);
DataCollection<Entity> workflow = _service.RetrieveMultiple(fetchxml).Entities;
if (workflow!=null && workflow.Count>0)
{
//删除旧业务流程
if (preBusProcessEntityNamee!=null)
{
string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='"+ preBusProcessEntityNamee + "'>" +
"<attribute name='businessprocessflowinstanceid' />" +
"<attribute name='bpf_name' />" +
"<attribute name='createdon' />" +
"<attribute name='bpf_"+ entityName + "id' />" +
"<order attribute='bpf_name' descending='false' />" +
"<filter type='and'>" +
"<condition attribute='bpf_"+ entityName + "id' operator='eq' value='{"+ entityId.ToString() + "}' />" +
"</filter>" +
"</entity>" +
"</fetch>";
FetchExpression preBusProcessEntityNameeXml = new FetchExpression(fetchXml);
DataCollection<Entity> preBusProcessEntityNameeEntities = _service.RetrieveMultiple(preBusProcessEntityNameeXml).Entities;
if (preBusProcessEntityNameeEntities!=null && preBusProcessEntityNameeEntities.Count>0)
{
_service.Delete(preBusProcessEntityNameeEntities[0].LogicalName, preBusProcessEntityNameeEntities[0].Id);
}
}
//绑定新的业务流程
SetProcessRequest SetProcess = new SetProcessRequest() {
Target = new EntityReference(entityName, entityId),
NewProcess= new EntityReference(workflow[0].LogicalName, workflow[0].Id)
};
_service.Execute(SetProcess);
}
}