繁体   English   中英

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 调用是异步的,所以我尝试等待它,使事件处理程序 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM