在业务流程

时间:2016-08-31 19:20:24

标签: javascript jquery dynamics-crm-2013 dynamics-crm-2016

我的业务流程有一个特殊的"验证。必须将3个步骤中的至少一个设置为"是"能够继续下一阶段。

这是我在舞台改变时的处理程序。

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技巧......哪些有缺点(错误和警告图标都显示在错误中,我必须隐藏错误我自己,有时当验证通过并进入下一阶段时,自定义错误消息将闪烁并消失。)

我试图让这部分内容显示自定义消息:

enter image description here

有更好的方法吗?我在考虑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,这是针对商机实体。

2 个答案:

答案 0 :(得分:1)

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

tableView

答案 1 :(得分:0)

我决定使用Notify.js并使用此网络资源执行相同操作,解决了重新显示通知的问题。

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