Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Enable instant update on Field without refreshing

(0) ShareShare
ReportReport
Posted on by 40

Hi , I have a setup for BPF together with JS where the opportunity stage & Form change based on a field in opportunity. When I make a change on that field, I need to click on the refresh button to bring up the new Form; ie...it is only working after clicking on the refresh button when update is made on that field. The problem is user input all others field & move on to next stage before clicking save or refresh, thus broken the workflow. How to I avoid this or enable an instant refresh when that particular field is change?

  • COOL Profile Picture
    40 on at
    RE: Enable instant update on Field without refreshing

    Hi Mehdi,

    The result is still the same only works for opportunity created via Lead to Opportunity Sales Process, but not via Opportunity Sales Process.

    Thanks.

  • Suggested answer
    meelamri Profile Picture
    13,212 User Group Leader on at
    RE: Enable instant update on Field without refreshing

    Hi COOL, 

    I have modified your code. It's not easy to make this logic work at the first time without debugging on your environment. I let you try this new code. We will debug together the logic of the code until it works. 

    var CreditUser = false;
    
    function form_onLoad(executionContext) {
    
        var formContext = executionContext.getFormContext();
    
        var product = formContext.getAttribute("new_productcre").getValue();
    
        //Reflow BPF whhen Product is Changed
        formContext.getAttribute('new_productcre').addOnChange(reflowBPF);
    
        var activeStage = formContext.data.process.getActiveStage();
    
        var originatingLead = formContext.getAttribute("originatingleadid").getValue();
    
        formContext.getControl("header_process_new_productnametext").setVisible(false);
    
        stageValidation(executionContext);
    
        if (product != null && product != "") {
    
            formContext.getAttribute("new_productnametext").setValue(product[0].name);
    
            formContext.ui.process.reflow(true);
    
        }
    
        formContext.getControl("new_promocode1").addPreSearch(function () {
    
            filterPromoCodeLookup(formContext, "new_promocode1", "100000000");
    
        });
    
        formContext.getControl("new_promocode2").addPreSearch(function () {
    
            filterPromoCodeLookup(formContext, "new_promocode2", "100000001");
    
        });
    
        formContext.getControl("new_promocode3").addPreSearch(function () {
    
            filterPromoCodeLookup(formContext, "new_promocode3", "100000002");
    
        });
    
        formContext.data.process.addOnStageChange(stageOnChange);
    
        if (originatingLead != null) {
    
            formContext.getControl("Leadtoopportunitysalesprocess").setVisible(true);
    
            formContext.getControl("opportunitysalesprocess").setVisible(false);
    
        } else {
    
            formContext.getControl("Leadtoopportunitysalesprocess").setVisible(false);
    
            formContext.getControl("opportunitysalesprocess").setVisible(true);
    
            formContext.ui.process.reflow(true);
    
        }
    
    }
    
    //Reflow BPF when Product is Changed
    function reflowBPF(executionContext) {
        var formContext = executionContext.getFormContext();
        formContext.ui.process.reflow(true);
    }
    
    function stageValidation(executionContext) {
    
        var formContext = executionContext.getFormContext();
    
        var product = formContext.getAttribute("new_productcre").getValue();
    
        var activeStage = formContext.data.process.getActiveStage();
    
        var userSettings = Xrm.Utility.getGlobalContext().userSettings;
    
        var loggedInUserID = userSettings.userId.slice(1, userSettings.userId.length - 1);
    
        var securityRoles = userSettings.roles._collection;
    
        var new_financetype = formContext.getAttribute("new_financetype").getValue();
    
        formContext.getControl("header_process_new_checked").setDisabled(true);
    
        formContext.getControl("header_process_new_checked_1").setDisabled(true);
    
        formContext.ui.process.reflow(true);
    
        for (var key in securityRoles) {
    
            if (securityRoles[key].name == "Cl Credit") {
    
                CreditUser = true;
    
            }
    
            if (securityRoles[key].name == "Cl Compliance" || securityRoles[key].name == "System Administrator") {
    
                formContext.getControl("header_process_new_checked").setDisabled(false);
    
                formContext.getControl("header_process_new_checked_1").setDisabled(false);
    
                formContext.ui.process.reflow(true);
    
            }
    
        }
    
        if (product != null && product != "") {
    
            if (product[0].name == "OB Type" && activeStage.getName() != "Prospecting") {
    
                MarkAllFieldReadOnly(formContext);
    
                formContext.ui.process.reflow(true);
    
            }
    
            else if (product[0].name == "AB Type" && activeStage.getName() != "Prospecting" && activeStage.getName() != "Proposal") {
    
                var lockFields = ["parentaccountid", "new_financetype", "transactioncurrencyid", "new_amountfinanced", "new_paymentfrequency", "new_term", "new_interestonly", "new_greaterthan80ltv", "new_lmi"];
    
                for (i = 0; i < lockFields.length; i  ) {
    
                    if (formContext.getControl(lockFields[i]) != null)
    
                        formContext.getControl(lockFields[i]).setDisabled(true);
    
                    formContext.ui.process.reflow(true);
    
                }
    
            }
    
            else {
    
                if (new_financetype == 100000011 || new_financetype == 100000012) {
    
                    formContext.getAttribute("new_assessmentform").setRequiredLevel("required");
    
                    formContext.getAttribute("new_disclosureform").setRequiredLevel("required");
    
                } else {
    
                    formContext.getAttribute("new_assessmentform").setRequiredLevel("none");
    
                    formContext.getAttribute("new_disclosureform").setRequiredLevel("none");
    
                    formContext.ui.process.reflow(true);
    
                }
    
            }
    
            if (!CreditUser && product[0].name == "AB Type") {
    
                //Credit assesment fields
    
                formContext.getControl("header_process_new_conditionalapproval").setDisabled(true);
    
                formContext.getControl("header_process_new_status").setDisabled(true);
    
                //Conditional Approved fields
    
                formContext.getControl("header_process_new_conditionscomments").setDisabled(true);
    
                // Credit Approved fields
    
                formContext.getControl("header_process_new_creditapprovalsupportingdocuments").setDisabled(true);
    
                formContext.getControl("header_process_new_policyexceptions").setDisabled(true);
    
                formContext.getControl("header_process_new_submittedinolas").setDisabled(true);
    
                formContext.getControl("header_process_new_policyexceptions").setDisabled(true);
    
                //common field
    
                formContext.getControl("header_process_new_allowstagechange").setDisabled(true);
    
                formContext.getControl("header_process_new_allowstagechange_1").setDisabled(true);
    
                formContext.getControl("header_process_new_allowstagechange_2").setDisabled(true);
    
                formContext.ui.process.reflow(true);
    
            }
    
        }
    
        if (product == "" || product == null) {
    
            if (new_financetype == 100000011 || new_financetype == 100000012) {
    
                formContext.getAttribute("new_assessmentform").setRequiredLevel("required");
    
                formContext.getAttribute("new_disclosureform").setRequiredLevel("required");
    
            } else {
    
                formContext.getAttribute("new_assessmentform").setRequiredLevel("none");
    
                formContext.getAttribute("new_disclosureform").setRequiredLevel("none");
    
                formContext.ui.process.reflow(true);
    
            }
    
        }
    
    }
    
    function stageOnChange(executionContext) {
    
        debugger;
    
        var formContext = executionContext.getFormContext();
    
        var activeStage = formContext.data.process.getActiveStage();
    
        var product = formContext.getAttribute("new_productcre").getValue();
    
        if (product != null && product != "" && product[0].name == "OB Type" && activeStage.getName() != "Prospecting") {
    
            MarkAllFieldReadOnly(formContext);
    
            formContext.ui.process.reflow(true);
    
        }
    
        else if (product != null && product != "" && CreditUser && product[0].name == "AB Type") {
    
            if (activeStage.getName() == "Conditional Approved" || activeStage.getName() == "Credit Assessment" || activeStage.getName() == "Credit Approved") {
    
                formContext.getControl("header_process_new_allowstagechange").setDisabled(false);
    
                formContext.getAttribute("new_allowstagechange").setValue(false);
    
            }
    
            if (activeStage.getName() != "Prospecting" && activeStage.getName() != "Proposal") {
    
                var lockFields = ["parentaccountid", "new_financetype", "transactioncurrencyid", "new_amountfinanced", "new_paymentfrequency", "new_term", "new_interestonly", "new_greaterthan80ltv", "new_lmi"];
    
                for (i = 0; i < lockFields.length; i  ) {
    
                    formContext.getControl(lockFields[i]).setDisabled(true);
    
                    formContext.ui.process.reflow(true);
    
                }
    
            }
    
        } else if (product != null && product != "" && !CreditUser && product[0].name == "AB Type") {
    
            //Added so, User without credit security role can not change two stages at a time.
    
            formContext.getAttribute("new_allowstagechange").setValue(false);
    
            formContext.ui.process.reflow(true);
    
        }
    
    }
    
    function MarkAllFieldReadOnly(formContext) {
    
        var allowEdit = ["name", "parentcontactid", "new_leadsource", "new_supplier", "new_financesubtype", "new_estimatedassetvalue", "new_interestonly", "new_greaterthan80ltv", "estimatedclosedate", "new_conditionalapproval", "new_lmi"]
    
        formContext.ui.controls.forEach(function (control, index) {
    
            if (allowEdit.indexOf(control.getName()) == -1) {
    
                control.setDisabled(true);
    
                formContext.ui.process.reflow(true);
    
            }
    
        });
    
    }
    
    function filterPromoCodeLookup(formContext, lookupField, Value) {
    
        var customerPromoCodeFilter = "";
    
        formContext.getControl(lookupField).addCustomFilter(customerPromoCodeFilter, "new_promocode");
    
    }
    
    function productAccount_onChange(executionContext) {
    
        var formContext = executionContext.getFormContext();
    
        var product = formContext.getAttribute("new_productcre").getValue();
    
        if (product != null && product != "") {
    
            formContext.getAttribute("new_productnametext").setValue(product[0].name);
    
        }
    
        stageValidation(executionContext);
    
        formContext.ui.process.reflow(true);
    
    }
    
    function financetype_onChange(executionContext) {
    
        var formContext = executionContext.getFormContext();
    
        var new_financetype = formContext.getAttribute("new_financetype").getValue();
    
        if (new_financetype == 100000011 || new_financetype == 100000012) {
    
            formContext.getAttribute("new_assessmentform").setRequiredLevel("required");
    
            formContext.getAttribute("new_disclosureform").setRequiredLevel("required");
    
        } else {
    
            formContext.getAttribute("new_assessmentform").setRequiredLevel("none");
    
            formContext.getAttribute("new_disclosureform").setRequiredLevel("none");
    
            //formContext.ui.process.reflow(true);
    
        }
    
    }
    
    // Ribbon functions
    
    function enableRibbonButtons() {
    
        // show/hide "Mark as Won" & "Reopen Opportunity" buttons based on security roles.
    
        var isAdmin = false;
    
        var userSettings = Xrm.Utility.getGlobalContext().userSettings;
    
        var loggedInUserID = userSettings.userId.slice(1, userSettings.userId.length - 1);
    
        var securityRoles = userSettings.roles._collection;
    
        for (var key in securityRoles) {
    
            if (securityRoles[key].name == "System Administrator") {
    
                isAdmin = true;
    
            }
    
        }
    
        return isAdmin;
    
    }

    Good Luck !

  • COOL Profile Picture
    40 on at
    RE: Enable instant update on Field without refreshing

    Hi Mehdi,

    please find the below, thanks.

    var CreditUser = false;

    function form_onLoad(executionContext){

       debugger;

       var formContext = executionContext.getFormContext();

       var product = formContext.getAttribute("new_productcre").getValue();

       var activeStage = formContext.data.process.getActiveStage();

       var originatingLead = formContext.getAttribute("originatingleadid").getValue();

       formContext.getControl("header_process_new_productnametext").setVisible(false);

       stageValidation(executionContext);

       if(product != null && product != ""){

        formContext.getAttribute("new_productnametext").setValue(product[0].name);

    formContext.ui.process.reflow(true);

       }

       formContext.getControl("new_promocode1").addPreSearch(function(){

           filterPromoCodeLookup(formContext,"new_promocode1","100000000");

       });

       formContext.getControl("new_promocode2").addPreSearch(function(){

           filterPromoCodeLookup(formContext,"new_promocode2","100000001");

       });

       formContext.getControl("new_promocode3").addPreSearch(function(){

           filterPromoCodeLookup(formContext,"new_promocode3","100000002");

       });

       formContext.data.process.addOnStageChange(stageOnChange);

       if(originatingLead != null){

           formContext.getControl("Leadtoopportunitysalesprocess").setVisible(true);

           formContext.getControl("opportunitysalesprocess").setVisible(false);

       }else{

            formContext.getControl("Leadtoopportunitysalesprocess").setVisible(false);

           formContext.getControl("opportunitysalesprocess").setVisible(true);

    formContext.ui.process.reflow(true);

       }

    }

    function stageValidation(executionContext){

    var formContext = executionContext.getFormContext();  

    var product = formContext.getAttribute("new_productcre").getValue();

     var activeStage = formContext.data.process.getActiveStage();  

    var userSettings = Xrm.Utility.getGlobalContext().userSettings;

    var loggedInUserID = userSettings.userId.slice(1,userSettings.userId.length-1);

    var securityRoles = userSettings.roles._collection;

    var new_financetype = formContext.getAttribute("new_financetype").getValue();

    formContext.getControl("header_process_new_checked").setDisabled(true);

    formContext.getControl("header_process_new_checked_1").setDisabled(true);

    formContext.ui.process.reflow(true);

     for(var key in securityRoles){

        if(securityRoles[key].name == "Cl Credit")

        {

            CreditUser = true;

        }

        if(securityRoles[key].name == "Cl Compliance" || securityRoles[key].name == "System Administrator"){

            formContext.getControl("header_process_new_checked").setDisabled(false);

            formContext.getControl("header_process_new_checked_1").setDisabled(false);

    formContext.ui.process.reflow(true);

        }

    }

    if(product != null && product != ""){

       if(product[0].name == "OB Type" && activeStage.getName() != "Prospecting"){

           MarkAllFieldReadOnly(formContext);

    formContext.ui.process.reflow(true);

       }

       else if(product[0].name == "AB Type" && activeStage.getName() != "Prospecting" && activeStage.getName() != "Proposal"){

           var lockFields = ["parentaccountid","new_financetype","transactioncurrencyid", "new_amountfinanced", "new_paymentfrequency", "new_term", "new_interestonly", "new_greaterthan80ltv", "new_lmi"];

           for(i=0;i<lockFields.length;i++){

               if(formContext.getControl(lockFields[i]) != null)

               formContext.getControl(lockFields[i]).setDisabled(true);

    formContext.ui.process.reflow(true);

           }

       }

       else {

           if(new_financetype == 100000011 || new_financetype == 100000012){

              formContext.getAttribute("new_assessmentform").setRequiredLevel("required");

               formContext.getAttribute("new_disclosureform").setRequiredLevel("required");

          }else{

              formContext.getAttribute("new_assessmentform").setRequiredLevel("none");

              formContext.getAttribute("new_disclosureform").setRequiredLevel("none");

    formContext.ui.process.reflow(true);

          }

       }

        if(!CreditUser && product[0].name == "AB Type"){

            //Credit assesment fields

              formContext.getControl("header_process_new_conditionalapproval").setDisabled(true);

              formContext.getControl("header_process_new_status").setDisabled(true);

              //Conditional Approved fields

              formContext.getControl("header_process_new_conditionscomments").setDisabled(true);

              // Credit Approved fields

              formContext.getControl("header_process_new_creditapprovalsupportingdocuments").setDisabled(true);

              formContext.getControl("header_process_new_policyexceptions").setDisabled(true);

              formContext.getControl("header_process_new_submittedinolas").setDisabled(true);

              formContext.getControl("header_process_new_policyexceptions").setDisabled(true);      

              //common field

              formContext.getControl("header_process_new_allowstagechange").setDisabled(true);

              formContext.getControl("header_process_new_allowstagechange_1").setDisabled(true);

              formContext.getControl("header_process_new_allowstagechange_2").setDisabled(true);

    formContext.ui.process.reflow(true);

        }

       }

       if(product == "" || product == null){

           if(new_financetype == 100000011 || new_financetype == 100000012){

              formContext.getAttribute("new_assessmentform").setRequiredLevel("required");

               formContext.getAttribute("new_disclosureform").setRequiredLevel("required");

          }else{

              formContext.getAttribute("new_assessmentform").setRequiredLevel("none");

              formContext.getAttribute("new_disclosureform").setRequiredLevel("none");

    formContext.ui.process.reflow(true);

          }

       }

    }

    function stageOnChange(executionContext){

       debugger;

       var formContext = executionContext.getFormContext();

       var activeStage = formContext.data.process.getActiveStage();    

       var product = formContext.getAttribute("new_productcre").getValue();

       if(product != null && product != "" && product[0].name == "OB Type" && activeStage.getName() != "Prospecting"){

           MarkAllFieldReadOnly(formContext);

    formContext.ui.process.reflow(true);

       }

       else if(product != null && product != "" && CreditUser && product[0].name == "AB Type"){

           if(activeStage.getName() == "Conditional Approved" || activeStage.getName() == "Credit Assessment" || activeStage.getName() == "Credit Approved"){        

               formContext.getControl("header_process_new_allowstagechange").setDisabled(false);

               formContext.getAttribute("new_allowstagechange").setValue(false);

           }

           if(activeStage.getName() != "Prospecting" && activeStage.getName() != "Proposal"){

               var lockFields = ["parentaccountid","new_financetype","transactioncurrencyid", "new_amountfinanced", "new_paymentfrequency", "new_term", "new_interestonly", "new_greaterthan80ltv", "new_lmi"];

               for(i=0;i<lockFields.length;i++){

                   formContext.getControl(lockFields[i]).setDisabled(true);

    formContext.ui.process.reflow(true);

               }

           }

       }else if(product != null && product != "" && !CreditUser && product[0].name == "AB Type"){

           //Added so, User without credit security role can not change two stages at a time.

           formContext.getAttribute("new_allowstagechange").setValue(false);

    formContext.ui.process.reflow(true);

       }

    }

    function MarkAllFieldReadOnly(formContext) {

       var allowEdit = ["name","parentcontactid","new_leadsource", "new_supplier", "new_financesubtype", "new_estimatedassetvalue", "new_interestonly", "new_greaterthan80ltv", "estimatedclosedate", "new_conditionalapproval", "new_lmi"]

       formContext.ui.controls.forEach(function (control, index) {

           if(allowEdit.indexOf(control.getName()) == -1){

               control.setDisabled(true);

    formContext.ui.process.reflow(true);

           }        

       });

    }

    function filterPromoCodeLookup(formContext, lookupField, Value){

       var customerPromoCodeFilter = "<filter type='and'><condition attribute='new_category' operator='eq' value='"+Value+"'/></filter>";

       formContext.getControl(lookupField).addCustomFilter(customerPromoCodeFilter, "new_promocode");

    }

    function productAccount_onChange(executionContext){

     var formContext = executionContext.getFormContext();

     var product = formContext.getAttribute("new_productcre").getValue();

      if(product != null && product != ""){

        formContext.getAttribute("new_productnametext").setValue(product[0].name);

       }

       stageValidation(executionContext);

    formContext.ui.process.reflow(true);

    }

    function financetype_onChange(executionContext){

    var formContext = executionContext.getFormContext();

    var new_financetype = formContext.getAttribute("new_financetype").getValue();

      if(new_financetype == 100000011 || new_financetype == 100000012){

              formContext.getAttribute("new_assessmentform").setRequiredLevel("required");

               formContext.getAttribute("new_disclosureform").setRequiredLevel("required");

          }else{

              formContext.getAttribute("new_assessmentform").setRequiredLevel("none");

              formContext.getAttribute("new_disclosureform").setRequiredLevel("none");

    //formContext.ui.process.reflow(true);

          }

    }

    // Ribbon functions

    function enableRibbonButtons(){

     // show/hide "Mark as Won" & "Reopen Opportunity" buttons based on security roles.

     var isAdmin = false;

     var userSettings = Xrm.Utility.getGlobalContext().userSettings;

     var loggedInUserID = userSettings.userId.slice(1,userSettings.userId.length-1);

     var securityRoles = userSettings.roles._collection;

     for(var key in securityRoles){

        if(securityRoles[key].name == "System Administrator")

        {

            isAdmin = true;

        }

     }

     return isAdmin;  

    }

    ref

  • meelamri Profile Picture
    13,212 User Group Leader on at
    RE: Enable instant update on Field without refreshing

    Hi COOL,

    Please share the complete code. This will help to find the source of the problem.

  • COOL Profile Picture
    40 on at
    RE: Enable instant update on Field without refreshing

    Hi Mehdi,

    Similar to the previous screenshot, the form change instantly when the Product field is updated if the workflow is under Lead to Opportunity Sales Process. But if the workflow is under Opportunity Sales Process, the form will only be updated when it is refresh or save. The workflow setup is same for both BPF.

    pastedimage1623027227562v1.png

    thanks.

  • meelamri Profile Picture
    13,212 User Group Leader on at
    RE: Enable instant update on Field without refreshing

    Hi COOL,

    Could you please share a screenshot where it works and another where it doesn't? This will help me to be more precise in my answer.

  • COOL Profile Picture
    40 on at
    RE: Enable instant update on Field without refreshing

    Hi Mehdi,

    I just found out that the instance update is not working on the form where the opportunity is created under the [Opportunity Sales Process] BPF whereas it is working fine for opportunity created via lead qualification under [Lead to Opportunity Sales Process].

    Any idea?

  • meelamri Profile Picture
    13,212 User Group Leader on at
    RE: Enable instant update on Field without refreshing

    I'm glad you were able to solve your problem. Don't forget to verify my answer by clicking on YES. This will close the discussion and allow people to find the solution quickly.

  • Suggested answer
    COOL Profile Picture
    40 on at
    RE: Enable instant update on Field without refreshing

    Hi Mehdi,

    I am not fully sure the "stageValidation()" do but probably to change some of the form as I don't know how to write js & just use the current one in the system & make the small changes. BTW I input formContext.ui.process.reflow(true) in few place, ie after each context where it related to the change on new_product  & the instant update is working well. Thank you.

  • Verified answer
    meelamri Profile Picture
    13,212 User Group Leader on at
    RE: Enable instant update on Field without refreshing

    Hi, 

    What does the function "stageValidation()" do?

    If you want to refresh the business process flow based on the new value changed by your JS code you will need to use formContext.ui.process.reflow(true) function. 

    I wrote a blog about this topic: https://xrmtricks.com/2021/04/13/how-to-set-up-complex-conditions-for-business-process-flow/

    Good luck!

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,137 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,879 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans