在业务流程中设置自定义警告消息

Set custom warning message in business process flow

我有一个具有 "special" 验证的业务流程。必须将 3 个步骤中的至少一个设置为 "Yes" 才能继续下一阶段。

这是我的换台负责人。

var subStageRequirements = new Array();
subStageRequirements.push({ stage: 0, fields: ['new_is_project_scope_defined_substage'] });
subStageRequirements.push({ stage: 1, fields: ['new_is_demo_1_substage', 'new_is_demo_2_substage', 'new_is_selection_srv_outlined_substage'] });
subStageRequirements.push({ stage: 2, fields: ['new_is_proposal_substage', 'new_is_negotiation_substage'] });


function onStageChange(context) {

    var stage = context.getEventArgs().getStage();
    var stageCategory = stage.getCategory().getValue();
    var direction = context.getEventArgs().getDirection();

    if (direction == "Next") {

        //validate requirements on previous field...
        var valid = false;
        var req = subStageRequirements[stageCategory - 1];

        for (var i = 0; i <= req.fields.length - 1; i++) {
            if (Xrm.Page.getAttribute(req.fields[i]).getValue()) {
                valid = true;
                break;
            }
        }

        if (!valid) {

            Xrm.Page.data.process.movePrevious(function (result) {
                if (result != "success") {
                    alert('Error forcing back to previous step: ' + result + '. Have fun...');
                }

                var messageArea = $(window.parent.document).find('.processWarningBar');
                messageArea.css('display', 'block');

                var textArea = $(window.parent.document).find('.processWarningBar-Text');

                textArea.attr('title', 'At least one sales sub-stage has to be selected to move on to next sales stage.');
                textArea.text('At least one sales sub-stage has to be selected to move on to next sales stage.');

                setTimeout(function () {
                    var messageArea = $(window.parent.document).find('.processWarningBar');
                    messageArea.css('display', 'none');
                }, 5000);
            });


        }

    }
}

如您所见,一旦阶段发生变化,我会检查上一阶段(当前阶段 - 1,位置)中的步骤是否至少有一个 true 值。

如果不是,我后退一个阶段(movePrevious)然后搞一些jQuery诡计......这有一些缺点(错误和警告图标都显示在错误中,我必须自己隐藏错误div,有时当验证通过并进入下一阶段时,自定义错误消息会闪烁并消失。

我正在尝试让这部分显示自定义消息:

有什么更好的方法吗?我在考虑 CRM 在显示自己的默认消息时调用的函数。我试图把它们找出来,但它们似乎隐藏得很好。

编辑

我按照以下答案的建议将验证码更改为此:

if (!valid) {

    Xrm.Page.data.process.movePrevious(function (result) {
        if (result != "success") {
            alert('Error forcing back to previous step: ' + result + '. Have fun...');
        }

        Xrm.Page.ui.setFormNotification('At least one sales sub-stage has to be selected to move on to next sales stage.', 'ERROR', 'subStageMessage');


        setTimeout(function () {
            Xrm.Page.ui.clearFormNotification('subStageMessage');
        }, 5000);

    });

    return;
}
else
{
    Xrm.Page.ui.clearFormNotification('subStageMessage'); //trying to hide it once the previous stage has passed validation, but error message is still shown...
}

现在的问题是即使进入下一阶段也会显示该消息。当验证失败时,用户将返回到上一步,然后显示消息。当用户完成缺失的步骤并进入下一阶段时,即使显示消息的代码未执行,消息仍会再显示 5 秒(??)

我正在使用 CRM 2016,这是针对商机实体的。

是的,存在更好的方法:Xrm.Page.ui.setFormNotification

// Sample
Xrm.Page.ui.setFormNotification("You have to complete required steps", "INFO", "messageId");

我决定使用 Notify.js 并通过使用此 Web 资源执行相同操作,重新出现通知的问题已解决。

if (!valid) {

    Xrm.Page.data.process.movePrevious(function (result) {
        Notify.add('<b>At least one sales sub-stage has to be selected to move on to next sales stage.</b>', 'WARNING', 'subStageMessage', null);
    });

 }
 else {
     //if previous stage's steps were all valid, remove all notifications
     Notify.remove();
 }