web
You’re offline. This is a read only version of the page.
close
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?

I have the same question (0)
  • meelamri Profile Picture
    13,216 User Group Leader on at

    Hi, 

    Would please share some screenshots and your code please ? 

  • COOL Profile Picture
    40 on at

    Hi Mehdi,

    Below is the screenshot where the opportunity stage change from Form1 to Form2 when the the value in the product field is change. But it only change when refresh button is clicked. 

    pastedimage1621322484691v1.png

    Below are the related code:

    function productAccount_onChange(executionContext){

     var formContext = executionContext.getFormContext();

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

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

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

       }

       stageValidation(executionContext);

    The above script have been added to the Event Handlers library for the function equal <productAccount_onChange>.

  • Verified answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    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!

  • Suggested answer
    COOL Profile Picture
    40 on at

    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.

  • meelamri Profile Picture
    13,216 User Group Leader on at

    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.

  • COOL Profile Picture
    40 on at

    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,216 User Group Leader on at

    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

    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,216 User Group Leader on at

    Hi COOL,

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

  • COOL Profile Picture
    40 on at

    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 62 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Pallavi Phade Profile Picture

Pallavi Phade 49

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans