Customer Engagement & Dynamics CRM Forum

  • 1.  How to verify (Account) form is loaded completely?

    Posted Oct 24, 2021 11:41 AM
    Hello,

    I'm trying to hide some options from an option set based on the option set value,
    The javascript is running correctly but I think it runs too fast that the form is not loading completely when the script already ran.

    I tried to put sometimes out and sometimes it works and sometimes it doesn't... Any ideas on how can I validate the form is loaded before I execute the JScript??

    I've tried to search but have not been able to figure it out, below is my code and I'm passing the execution context

    function updateOptionSet(executionContext) {
    var formContext = executionContext.getFormContext();
    var optionValue = formContext.getAttribute("allgnt_customercategorycode2").getValue();
    if (optionValue == null || optionValue == 739280000 || optionValue == 739280001) {
    var optionControl = formContext.getControl("allgnt_customercategorycode2");
    formContext.getControl("allgnt_customercategorycode2").removeOption(930670000);
    formContext.getControl("allgnt_customercategorycode2").removeOption(930670001);
    formContext.getControl("allgnt_customercategorycode2").removeOption(930670002);
    formContext.getControl("allgnt_customercategorycode2").removeOption(930670003);
    }
    }

    Thanks for the help!!

    ------------------------------
    Antonio Cordero
    Business Applications Analyst
    FinishMaster
    IN
    ------------------------------


  • 2.  RE: How to verify (Account) form is loaded completely?

    MICROSOFT MVP
    Posted Oct 25, 2021 08:55 AM
    Could you share more details about what validation issues you're having , as well as what you meant by this: "I tried to put sometimes out"?

    I'm assuming this logic is being called from the form onLoad?  Also note you can simplify the calls slightly by not getting the control multiple times:
    function updateOptionSet(executionContext) {
    	var formContext = executionContext.getFormContext();
    	var optionValue = formContext.getAttribute("allgnt_customercategorycode2").getValue();
    	if (optionValue == null || optionValue == 739280000 || optionValue == 739280001) {
    		var optionControl = formContext.getControl("allgnt_customercategorycode2");
    		optionControl.removeOption(930670000);
    		optionControl.removeOption(930670001);
    		optionControl.removeOption(930670002);
    		optionControl.removeOption(930670003);
    	}
    }
    ​

    The more detailed information about what is failing, the better.  I'm guessing the only why it is not removing the options when it should is that you have the values wrong in your if statement, or there is more than one control on the form, and so you need to be looping through the controls for the allgnt_customercategorycode2 attribute. (btw it's been awhile since I've seen that allgnt prefix 😜 )

    ------------------------------
    Daryl LaBar
    President, MVP
    Gap Integrity
    Fishers, IN
    ------------------------------



  • 3.  RE: How to verify (Account) form is loaded completely?

    GOLD CONTRIBUTOR
    Posted Oct 25, 2021 09:32 AM
    Hi Antonio,

    We do not use jQuery to check if the form is fully loaded with D365 forms. There are a few things for you to check up:
    • Make sure it called on OnLoad event
    • "Pass execution context as first parameter" checkbox is checked
    • You may add debugger statement in you code or just add alert to show some messages for debugging
    • Double check the control name and optionset values

    It works to us, I believe it will work for you.

    By the way, Instead of using one control to change its own behaviour based upon its selections, I would suggest the business to use two controls: first control will change second control's behaviour by different selections, in which way the logic would be simpler and cleaner.

    ------------------------------
    Junbin Duan
    Senior Application Developer
    National Research Council Canada
    Ottawa ON
    ------------------------------



  • 4.  RE: How to verify (Account) form is loaded completely?

    Posted Oct 25, 2021 10:12 AM
    it is extremely rare already check and it should work... I actually do that for 2 option sets and for one works but not for the other.
    Same function

    ------------------------------
    Antonio Cordero
    Business Applications Analyst
    FinishMaster
    IN
    ------------------------------



  • 5.  RE: How to verify (Account) form is loaded completely?

    MICROSOFT MVP
    Posted Oct 25, 2021 10:50 AM
    If it happens sometimes and not others it could be as simple as some other JS error occurring that stops execution after the first option set is updated, but not before the second one has a chance to.

    ------------------------------
    Daryl LaBar
    President, MVP
    Gap Integrity
    Fishers IN
    ------------------------------



  • 6.  RE: How to verify (Account) form is loaded completely?

    TOP CONTRIBUTOR
    Posted Oct 26, 2021 02:09 PM

    You need to add a function timeout to addoption and removeoption.  I am just giving some Code snippets to help you understand my process.  We created our own special functions that use Xrm.Page or FrmCtrl methods:  I will set them in Bold Blue to highlight them.

    The Main functions you need to be conerned with will be in Bold Purple.  Hopefully , what I have maopped out makes sense

    // Here I am setting up some Globals:

    //#region Globals
    var _FormType = null;
    var _OrderStatusOptions;
    var _OrderStatus = 0;
    var _Role = "None";
    //#endregion Globals

     

    // This Function fills in the Global Variables

    function InitializeFields() {

       var _PreOrderStatusOption2;

       _FormType = _FormCtrl.ui.getFormType();

       _OrderStatusOptions = getOptionSetFromControl("new_orderstatus");

       _OrderStatusOption2 = _FormCtrl.ui.controls.get("new_orderstatus");

       _StatusOptions = {};

       _StatusOptions.InProcess = _PreOrderStatusOption2.getAttribute().getOption(1);

       _StatusOptions.Rejected = _PreOrderStatusOption2.getAttribute().getOption(2);

       _StatusOptions.Completed = _PreOrderStatusOption2.getAttribute().getOption(3);

    }

     

     function AddOptionTimeOut(OptionField, OptionValue, TimeoutLength) {

       setTimeout(function () {

          _FormCtrl.getControl(OptionField).addOption(_OrderStatusOptions[OptionValue]);

       }, TimeoutLength);

    }

    function RemoveOptionTimeOut(OptionField, OptionValue, TimeoutLength) {

       setTimeout(function () {

          _FormCtrl.getControl(OptionField).removeOption(OptionValue);

       }, TimeoutLength);

    }

    // Here is where I add and remove options 

    function new_orderstatus_onchange(new_orderstatus, new_orderstatusValue) {

       let FunctionTimeOut = 25;  // ß This is the Timeout Value.  25 works for me, but if it still doesn't work, increase the value

        // reset the OptionSet Values and then fill them in again so all Options are available
     
       ltfClearOptions("new_orderstatus");  

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

          if (_OrderStatusOptions[i].text !== "") {

             ltfAddOption(ltfGetControl("new_orderstatus"), _OrderStatusOptions[i]);

          }

       }

       if (_Role != "Admin") {

           switch (new_orderstatusValue) {

              case 1:  // _StatusOptions.InProcess 

                   RemoveOptionTimeOut("new_orderstatus", 2, FunctionTimeOut);

            break;

             case 2:  //  _StatusOptions.Rejected

                   RemoveOptionTimeOut("new_orderstatus", 1, FunctionTimeOut);

                    break;

             case 3:  // _StatusOptions.Completed
       
               RemoveOptionTimeOut("new_orderstatus", 1, FunctionTimeOut);

                  RemoveOptionTimeOut("new_orderstatus", 2, FunctionTimeOut);
             break;

          default:

             break;
          }

       }
    }



    ------------------------------
    Gerry Yurko
    CRM Developer
    Crown Castle Fiber Networks
    Boxborough MA
    ------------------------------



If you've found this thread useful, dive deeper into User Group community content by role