Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Preventing BPF to move to next stage without satisfying the condition on the first stage

Posted on by 100

Hi All,

I have a scenario in the Business process flow to not to move to the next stage until it satisfies the condition in the current stage.

I have 5 stages and in each stage, I have a condition to meet. Once it is met, the BPF should allow it to move on to the next stage.  

The first BPF stage name is "verify Contact". 

This stage is having a field "verify Contact" in the BPF header. This has a drop-down list and when the user selects "Approved", it should get move to the next stage (or) else it should throw an error as  " Since Verify contact is not approved, you cannot move to next stage".

Can anyone please help me with how to do this in javascript?

please refer to the screenshot for more details.

pastedimage1564700976130v2.png

*This post is locked for comments

  • chauhanhardik Profile Picture
    chauhanhardik 685 on at
    RE: Preventing BPF to move to next stage without satisfying the condition on the first stage

    Hi Gautam,

    I used the same code, but getting an error after whole code gets executed. please have a look at my code and let me know if you find any error.

    Error:
    UciError: Required parameter is null or undefined: handler

    Code:

    function setStatusReason(executionContext) {
        debugger;
        var formContext = executionContext.getFormContext();
        if (formContext != "" && formContext != undefined && formContext != null) {
            formContext.data.process.addOnStageChange(getStage(formContext)); // Trigger the function when move to next stage.
        }
    }
    function getStage(formContext) {
        debugger;
        var entityName = formContext.data.entity.getEntityName();
        var activeStage = formContext.data.process.getActiveStage();
        var stagename = activeStage.getName();
        switch (stagename) {
            case "New":
                if (entityName == "nt_interstatetransfers") {
                    formContext.getAttribute("statuscode").setValue(206400005);
                } else {
                    formContext.getAttribute("statuscode").setValue(206400004);
                }
                break;
            case "Review Required":
                if (entityName == "nt_interstatetransfers") {
                    formContext.getAttribute("statuscode").setValue(206400004);
                } else {
                    formContext.getAttribute("statuscode").setValue(1);
                }
                break;
            case "HR Approval":
                if (entityName == "nt_employee") {
                    formContext.getAttribute("statuscode").setValue(206400006);
                } else {
                    formContext.getAttribute("statuscode").setValue(206400001);
                }
                break;
            case "Payroll Review":
                formContext.getAttribute("statuscode").setValue(206400002);
                break;
            case "Payroll Approval":
                formContext.getAttribute("statuscode").setValue(206400003);
                break;
            default:
                formContext.getAttribute("statuscode").setValue(1);
        }
    }
    any help here is much appreciated.
    Thanks,
    Hardik Chauhan
  • Poornimaa Profile Picture
    Poornimaa 100 on at
    RE: Preventing BPF to move to next stage without satisfying the condition on the first stage

    Hi Goutam,

    Thank you for your help. !! Much appreciated.

    JS Worked well with all my stages.

    Cheers,

    Poornimaa

  • RE: Preventing BPF to move to next stage without satisfying the condition on the first stage

    Hi Goutam,

    Thanks.

    I was checking this and came up with similar JS logic.

    Works perfectly for me!!

    Thanks

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Preventing BPF to move to next stage without satisfying the condition on the first stage

    You can try with JS code -

    function onLoad() {

    Xrm.Page.data.process.addOnStageChange(getStage);

    }

    function getStage() {

    var activeStage = Xrm.Page.data.process.getActiveStage();

    var stageId = activeStage.getId();

    var stageName = activeStage.getName();

    If (stageName=="stagename")

    {

    if (Check the optionset value )

    {

    // Set previous stage as active stage

    //Set form notification

    }

    }

    }

  • Poornimaa Profile Picture
    Poornimaa 100 on at
    RE: Preventing BPF to move to next stage without satisfying the condition on the first stage

    This feature is available only on Dynamics 365 business process flow(Adding workflow within BPF) and not on CRM 2016. We are using CRM 2016.

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Preventing BPF to move to next stage without satisfying the condition on the first stage

    That is not correct.  The workflow needs to be trigger by the stage change and not when the field change. This is why it is triggering when you are saving the record.

    If you look at the link I shared initialy, they talk about creating a vworkflow which triggers on demand and then you select that workflow within the BPF.

    www.magnetismsolutions.com/.../how-to-run-workflow-on-business-process-flow-stage-change-in-dynamics-365

  • Poornimaa Profile Picture
    Poornimaa 100 on at
    RE: Preventing BPF to move to next stage without satisfying the condition on the first stage

    Hi Ravi, thanks for your response.

    I disabled the workflow and I changed the status to "Waiting for customer information " and then when I click on the "SAVE", it is all fine.

    But when I enable the Workflow, it is behaving like the same and throws an error.

    When I have all the information, then trainee section validated == "Approved" or else one of the options in the option set. The workflow triggers when this field changes. So I believe when I change to something else apart from "Approved", it is throwing me an error irrespective of moving of next stage. ? 

    Screenshot of Workflow.

    pastedimage1564969092774v1.png

    pastedimage1564969150829v2.png

    pastedimage1564969191408v3.png

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Preventing BPF to move to next stage without satisfying the condition on the first stage

    Hi Poornima,

    This should not happen if you have register the workflow only on stage change for that stage. It looks like you have some other code/logic which try to change the stage to next on saving the record which internally triggering your workflow.

    Can you check if this is the case.? If you can't find anything which is changing the stage, then disable the workflow on stage change and then update the record and check if the stage gets changed. This will confirm if there is some code which is changing the stage automatically or not.

    Hope this helps.

  • Poornimaa Profile Picture
    Poornimaa 100 on at
    RE: Preventing BPF to move to next stage without satisfying the condition on the first stage

    Hello,

    The above solution worked well, but there is another problem.!!

    The BPF throws me an error when I'm not selecting "Approved" and try to move to the next stage, which is good.

    But it is not allowing me to save on the first stage itself if it is not approved. There is a scenario if all the fields in that first stage are filled, then make the status as Approved, or else "waiting for the information from Contact" (or) "waiting from Customer" option needs to be selected.

    When I'm selecting one of these options and click "SAVE", it is showing the error "You cannot move to next stage............", but actually here I'm not moving to the next stage, but just saving the information. 

    Can you please help me?

    pastedimage1564965347277v6.png

  • Poornimaa Profile Picture
    Poornimaa 100 on at
    RE: Preventing BPF to move to next stage without satisfying the condition on the first stage

    Thank you so much, Ravi. Your help is much appreciated. !!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans