![](/img/trans.png)
[英]Update Business Process Flow specific details in Dynamics CRM project
[英]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 調用是異步的,所以我嘗試等待它,使事件處理程序 function 異步,但它似乎也不是那樣工作的。
我嘗試的另一件事是將錯誤處理代碼放入 WebAPI 調用的成功 function 中,但如果我這樣做,我將無法訪問外部事件,因此executionContext.getEventArgs().preventDefault();
停止工作。
我怎樣才能讓我的錯誤處理代碼在特定階段運行,這樣我仍然可以阻止用戶進入下一階段?
Xrm.WebApi.retrieveMultipleRecords
是異步執行的function。 它返回一個 Promise,用於鏈接后續的異步處理程序。
對此 promise function 的調用在執行其異步代碼之前將控制返回給線程。 因此,在正確設置stages
之前執行var socioeconomicStatus = stages.find()
行。
一旦進入 promise 鏈,您必須通過它 go 直到結束,如下所示:
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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.