Oct 4, 2017

Validate Next Stage button of Business Process Flow

In Business Process Flows in Dynamics 365, we can proceed to next stage by clicking Next Stage button, if all the required fields in the current stage is filled. This is a straight forward validation. Anyway, we may need to do bit more tricky validations through JavaScript.

Let’s see the approach for adding a client script for this event. Please check below code snippet. As you may notice Stage Name and Direction are giving us most needed values to identify when we need to execute our logic.

function onLoad()
{
    Xrm.Page.data.process.addOnStageChange(OnStageProgress);
}

function OnStageProgress(executionContext)
{
    var stageName = Xrm.Page.data.process.getActiveStage().getName();
    var direction = executionContext.getEventArgs().getDirection();
    alert(stageName);
    alert(direction); //Values are Next OR Previous
}

Consider this scenario for an example. I need to move to next stage which is Complete stage if either of Description or Company Name is filled. Let’s extend the logic for that;

function OnStageProgress(executionContext) {
    var stageName = Xrm.Page.data.process.getActiveStage().getName();
    var direction = executionContext.getEventArgs().getDirection();

    var description = Xrm.Page.getAttribute('description').getValue();
    var company = Xrm.Page.getAttribute('parentcustomerid').getValue();

    if ((direction == 'Next') && (stageName == 'Complete'))
    {
    if ((description == null) && (company == null))
    {
        Xrm.Page.data.process.movePrevious(function () {
        Xrm.Page.ui.setFormNotification('Either Description OR Company is required', 'ERROR');
        });
    }
    }
}

Here we get the error message as expected.


Anyway, we need to add Xrm.Page.ui.clearFormNotification(); in onSave event so notices will be cleared when filling either of fields.

1 comment:

  1. Nice work Sumedha. It works like gem, but there is one cliche in this approach. From the end user point, they see a movement of stage and then it came back to the current stage. Is there any way we can avoid that. I know from client side perspective it is quite hard to do!! But any luck we can achieve without that cliche?

    ReplyDelete