简体   繁体   English

Dynamics 365 WebAPI 获取业务流程阶段

[英]Dynamics 365 WebAPI getting Business Process Flow stages

So I am trying to execute a piece of code on preStageChange event of a Business Process Flow.所以我试图在业务流程的 preStageChange 事件上执行一段代码。

For that, I tried to get the workflow ID using the WebAPI, then get the stages of the workflow.为此,我尝试使用 WebAPI 获取工作流 ID,然后获取工作流的阶段。 If I would manage to do that, I could write a simple if statement to see if I am on the desired stage.如果我设法做到这一点,我可以编写一个简单的 if 语句来查看我是否处于所需的阶段。

I tried this:我试过这个:

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");
        }
    }
}

Basically I am trying to execute the code when I am on the "Socioeconomic Status" stage, before going to the next stage.基本上,在进入下一阶段之前,我试图在处于“社会经济地位”阶段时执行代码。 The reason I'm trying to do this is because I want to prevent the user from going to the next stage if they made any errors while filling out the form.我尝试这样做的原因是因为我想阻止用户在填写表格时犯任何错误而进入下一阶段。

I realize that the WebAPI call I am using is async, so I tried awaiting it, making the event handler function async, but it doesn't seem to work that way either.我意识到我正在使用的 WebAPI 调用是异步的,所以我尝试等待它,使事件处理程序 function 异步,但它似乎也不是那样工作的。

Another thing I tried is placing the error handling code into the success function of the WebAPI call, but if I do that I can't access the outer event so executionContext.getEventArgs().preventDefault();我尝试的另一件事是将错误处理代码放入 WebAPI 调用的成功 function 中,但如果我这样做,我将无法访问外部事件,因此executionContext.getEventArgs().preventDefault(); stops working.停止工作。

How can I make my error handling code run on a specific stage so that I can still prevent the user from going to the next stage?我怎样才能让我的错误处理代码在特定阶段运行,这样我仍然可以阻止用户进入下一阶段?

Xrm.WebApi.retrieveMultipleRecords is a function that is executed asynchronously. Xrm.WebApi.retrieveMultipleRecords是异步执行的function。 It returns a Promise that is intended to chain subsequent asynchronous handlers.它返回一个 Promise,用于链接后续的异步处理程序。

A call to this promise function returns control to the thread before its asynchronous code is executed.对此 promise function 的调用在执行其异步代码之前将控制返回给线程。 Therefore the line var socioeconomicStatus = stages.find() is executed before stages is set properly.因此,在正确设置stages之前执行var socioeconomicStatus = stages.find()行。

Once in a promise chain you have to go through it to the end like this:一旦进入 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);
    });

As you can see, there is only one error handler.如您所见,只有一个错误处理程序。 When the first query fails, it automatically skips the .then functions and proceeds with the .catch .当第一个查询失败时,它会自动跳过.then函数并继续执行.catch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更新Dynamics CRM项目中特定于业务流程的详细信息 - Update Business Process Flow specific details in Dynamics CRM project Dynamics CRM 365 WebApi 8.2 AddListMembersList 方法 - Dynamics CRM 365 WebApi 8.2 AddListMembersList method 使用客户端脚本在Dynamics CRM中的Opportunity上设置业务流程阶段 - Use client-side script to set Business Process Flow Stage on Opportunity in Dynamics CRM 当在Dynamic 365 Online中触发OnStageChange事件时,如何确定用户是转到业务流程的下一阶段还是上一步? - How can I tell if user is moving to next or previous stage of business process flow when OnStageChange event is fired in Dynamic 365 Online? Dynamics Crm 365 webapi - 通过 javascript 发布网络资源 - Dynamics Crm 365 webapi - publish webresource via javascript Dynamics 365 CE - 如何在 Javascript 中使用 WebApi 取消案例? - Dynamics 365 CE - How to cancel a case using WebApi in Javascript? 在业务流程中设置自定义警告消息 - Set custom warning message in business process flow 业务流程流OptionSetValue步骤上的过滤选项 - Filtering options on Business Process flow OptionSetValue step 使用 Business Process Flow Next Stage 按钮触发电源自动化流程 - Trigger power automate flow with Business Process Flow Next Stage button 在 JS-Dynamics 365 中使用“Xrm.WebApi.createRecord”创建实体记录 - Create an Entity Record using "Xrm.WebApi.createRecord" in JS- Dynamics 365
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM