Dynamics 365 WebAPI 获取业务流程阶段
Dynamics 365 WebAPI getting Business Process Flow stages
所以我试图在业务流程的 preStageChange 事件上执行一段代码。
为此,我尝试使用 WebAPI 获取工作流 ID,然后获取工作流的阶段。
如果我设法做到这一点,我可以编写一个简单的 if 语句来查看我是否处于所需的阶段。
我试过这个:
function preStageOnChange(executionContext) {
var formContext = executionContext.getFormContext();
var activeStage = formContext.data.process.getActiveStage();
var stages = null;
Xrm.WebApi.retrieveMultipleRecords("workflow", "?$filter=name eq 'Customer Onboarding'&$select=uniquename&$top=1").then(
function success(result) {
var workflowId = result.entities[0]["workflowid"];
Xrm.WebApi.retrieveMultipleRecords("processstage", "?$select=stagename&$filter=processid/workflowid eq " + workflowId).then(
function success(result) {
stages = result.entities;
},
function (error) {
console.log(error.message);
}
);
},
function (error) {
console.log(error.message)
}
);
var socioeconomicStatus = stages.find(stage => stage["stagename"] == "Socioeconomic Status");
if(activeStage.getId().toString() == socioeconomicStatus["processstageid"]){
if (formContext.getAttribute("ava_netincome").getValue() == null && formContext.getAttribute("ava_investmentincome").getValue() == null && formContext.getAttribute("ava_otherincome").getValue() == null) {
formContext.ui.setFormNotification("At least one income value must be populated", "ERROR", "income_error"); //sets form notification in top of the form
executionContext.getEventArgs().preventDefault(); //prevent navigation to next stage
}
else {
formContext.ui.clearFormNotification("income_error");
}
if (formContext.getAttribute("ava_householdexpenses").getValue() == null && formContext.getAttribute("ava_otherexpenses").getValue() == null) {
formContext.ui.setFormNotification("At least one expense value must be populated", "ERROR", "expense_error"); //sets form notification in top of the form
executionContext.getEventArgs().preventDefault(); //prevent navigation to next stage
}
else {
formContext.ui.clearFormNotification("expense_error");
}
if (formContext.getAttribute("ava_currentloan").getValue() != null && formContext.getAttribute("ava_loanexpenses").getValue() == null)
{
formContext.ui.setFormNotification("Loan expenses must be populated", "ERROR", "loan_error"); //sets form notification in top of the form
executionContext.getEventArgs().preventDefault(); //prevent navigation to next stage
}
else {
formContext.ui.clearFormNotification("loan_error");
}
}
}
基本上,在进入下一阶段之前,我试图在处于“社会经济地位”阶段时执行代码。我尝试这样做的原因是因为我想阻止用户在填写表格时出现任何错误而进入下一阶段。
我意识到我正在使用的 WebAPI 调用是异步的,所以我尝试等待它,使事件处理程序函数异步,但它似乎也不能那样工作。
我尝试的另一件事是将错误处理代码放入 WebAPI 调用的成功函数中,但如果这样做,我将无法访问外部事件,因此 executionContext.getEventArgs().preventDefault();
停止工作。
如何在特定阶段制作我的错误处理代码运行,这样我仍然可以阻止用户进入下一阶段?
Xrm.WebApi.retrieveMultipleRecords
是一个异步执行的函数。它 returns 一个旨在链接后续异步处理程序的 Promise。
调用此 promise 函数 returns 在执行异步代码之前控制线程。因此 var socioeconomicStatus = stages.find()
行在 stages
设置正确之前执行。
一旦进入承诺链,您必须像这样通过它直到结束:
Xrm.WebApi.retrieveMultipleRecords("workflow", "?$filter=name eq 'Customer Onboarding'&$select=uniquename&$top=1")
.then(result => {
const workflowId = result.entities[0]["workflowid"];
return Xrm.WebApi.retrieveMultipleRecords("processstage", "?$select=stagename&$filter=processid/workflowid eq " + workflowId);
})
.then(result => {
var socioeconomicStatus = result.entities.find(stage => stage["stagename"] == "Socioeconomic Status");
if (activeStage.getId().toString() == socioeconomicStatus["processstageid"]) {
if (formContext.getAttribute("ava_netincome").getValue() == null && formContext.getAttribute("ava_investmentincome").getValue() == null && formContext.getAttribute("ava_otherincome").getValue() == null) {
formContext.ui.setFormNotification("At least one income value must be populated", "ERROR", "income_error"); //sets form notification in top of the form
executionContext.getEventArgs().preventDefault(); //prevent navigation to next stage
}
else {
formContext.ui.clearFormNotification("income_error");
}
if (formContext.getAttribute("ava_householdexpenses").getValue() == null && formContext.getAttribute("ava_otherexpenses").getValue() == null) {
formContext.ui.setFormNotification("At least one expense value must be populated", "ERROR", "expense_error"); //sets form notification in top of the form
executionContext.getEventArgs().preventDefault(); //prevent navigation to next stage
}
else {
formContext.ui.clearFormNotification("expense_error");
}
if (formContext.getAttribute("ava_currentloan").getValue() != null && formContext.getAttribute("ava_loanexpenses").getValue() == null) {
formContext.ui.setFormNotification("Loan expenses must be populated", "ERROR", "loan_error"); //sets form notification in top of the form
executionContext.getEventArgs().preventDefault(); //prevent navigation to next stage
}
else {
formContext.ui.clearFormNotification("loan_error");
}
}
})
.catch(error => {
console.log(error.message);
});
如您所见,只有一个错误处理程序。当第一个查询失败时,它会自动跳过 .then
函数并继续执行 .catch
.
所以我试图在业务流程的 preStageChange 事件上执行一段代码。
为此,我尝试使用 WebAPI 获取工作流 ID,然后获取工作流的阶段。 如果我设法做到这一点,我可以编写一个简单的 if 语句来查看我是否处于所需的阶段。
我试过这个:
function preStageOnChange(executionContext) {
var formContext = executionContext.getFormContext();
var activeStage = formContext.data.process.getActiveStage();
var stages = null;
Xrm.WebApi.retrieveMultipleRecords("workflow", "?$filter=name eq 'Customer Onboarding'&$select=uniquename&$top=1").then(
function success(result) {
var workflowId = result.entities[0]["workflowid"];
Xrm.WebApi.retrieveMultipleRecords("processstage", "?$select=stagename&$filter=processid/workflowid eq " + workflowId).then(
function success(result) {
stages = result.entities;
},
function (error) {
console.log(error.message);
}
);
},
function (error) {
console.log(error.message)
}
);
var socioeconomicStatus = stages.find(stage => stage["stagename"] == "Socioeconomic Status");
if(activeStage.getId().toString() == socioeconomicStatus["processstageid"]){
if (formContext.getAttribute("ava_netincome").getValue() == null && formContext.getAttribute("ava_investmentincome").getValue() == null && formContext.getAttribute("ava_otherincome").getValue() == null) {
formContext.ui.setFormNotification("At least one income value must be populated", "ERROR", "income_error"); //sets form notification in top of the form
executionContext.getEventArgs().preventDefault(); //prevent navigation to next stage
}
else {
formContext.ui.clearFormNotification("income_error");
}
if (formContext.getAttribute("ava_householdexpenses").getValue() == null && formContext.getAttribute("ava_otherexpenses").getValue() == null) {
formContext.ui.setFormNotification("At least one expense value must be populated", "ERROR", "expense_error"); //sets form notification in top of the form
executionContext.getEventArgs().preventDefault(); //prevent navigation to next stage
}
else {
formContext.ui.clearFormNotification("expense_error");
}
if (formContext.getAttribute("ava_currentloan").getValue() != null && formContext.getAttribute("ava_loanexpenses").getValue() == null)
{
formContext.ui.setFormNotification("Loan expenses must be populated", "ERROR", "loan_error"); //sets form notification in top of the form
executionContext.getEventArgs().preventDefault(); //prevent navigation to next stage
}
else {
formContext.ui.clearFormNotification("loan_error");
}
}
}
基本上,在进入下一阶段之前,我试图在处于“社会经济地位”阶段时执行代码。我尝试这样做的原因是因为我想阻止用户在填写表格时出现任何错误而进入下一阶段。
我意识到我正在使用的 WebAPI 调用是异步的,所以我尝试等待它,使事件处理程序函数异步,但它似乎也不能那样工作。
我尝试的另一件事是将错误处理代码放入 WebAPI 调用的成功函数中,但如果这样做,我将无法访问外部事件,因此 executionContext.getEventArgs().preventDefault();
停止工作。
如何在特定阶段制作我的错误处理代码运行,这样我仍然可以阻止用户进入下一阶段?
Xrm.WebApi.retrieveMultipleRecords
是一个异步执行的函数。它 returns 一个旨在链接后续异步处理程序的 Promise。
调用此 promise 函数 returns 在执行异步代码之前控制线程。因此 var socioeconomicStatus = stages.find()
行在 stages
设置正确之前执行。
一旦进入承诺链,您必须像这样通过它直到结束:
Xrm.WebApi.retrieveMultipleRecords("workflow", "?$filter=name eq 'Customer Onboarding'&$select=uniquename&$top=1")
.then(result => {
const workflowId = result.entities[0]["workflowid"];
return Xrm.WebApi.retrieveMultipleRecords("processstage", "?$select=stagename&$filter=processid/workflowid eq " + workflowId);
})
.then(result => {
var socioeconomicStatus = result.entities.find(stage => stage["stagename"] == "Socioeconomic Status");
if (activeStage.getId().toString() == socioeconomicStatus["processstageid"]) {
if (formContext.getAttribute("ava_netincome").getValue() == null && formContext.getAttribute("ava_investmentincome").getValue() == null && formContext.getAttribute("ava_otherincome").getValue() == null) {
formContext.ui.setFormNotification("At least one income value must be populated", "ERROR", "income_error"); //sets form notification in top of the form
executionContext.getEventArgs().preventDefault(); //prevent navigation to next stage
}
else {
formContext.ui.clearFormNotification("income_error");
}
if (formContext.getAttribute("ava_householdexpenses").getValue() == null && formContext.getAttribute("ava_otherexpenses").getValue() == null) {
formContext.ui.setFormNotification("At least one expense value must be populated", "ERROR", "expense_error"); //sets form notification in top of the form
executionContext.getEventArgs().preventDefault(); //prevent navigation to next stage
}
else {
formContext.ui.clearFormNotification("expense_error");
}
if (formContext.getAttribute("ava_currentloan").getValue() != null && formContext.getAttribute("ava_loanexpenses").getValue() == null) {
formContext.ui.setFormNotification("Loan expenses must be populated", "ERROR", "loan_error"); //sets form notification in top of the form
executionContext.getEventArgs().preventDefault(); //prevent navigation to next stage
}
else {
formContext.ui.clearFormNotification("loan_error");
}
}
})
.catch(error => {
console.log(error.message);
});
如您所见,只有一个错误处理程序。当第一个查询失败时,它会自动跳过 .then
函数并继续执行 .catch
.