Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Setting Field To Required in Different Tab through Javascript

(0) ShareShare
ReportReport
Posted on by 15

I have the following situation:

In a custom entity form, I have 2 tabs. In the first tab, there is an option set field. Based on a particular value that is chosen, a field in the other tab gets set to required and vice versa.

Now, when I load the form and change the option set field WITHOUT toggling to the second tab, it sets the field required / unrequired as expected.


However, as soon as I switch to the second tab, then switch back to my first and change the option set  field again, the 2nd tab field no longer updates. I can see the code fetching that control and setting the requirement level through the debugger, yet the save / form does not reflect the field's updated requirement status. I've also tried doing this through a tabonchange event, but see the same behavior and that is not desirable anyways.

Note that everything works fine if both fields are in the same tab, but the requirement is to have them in separate tabs.

Has anybody seen this behavior? It seems like a bug, but I'm not sure. If there are workarounds, I would appreciate it! Business rules won't work because this logic needs to be in place before the record is saved.

  • Vipin7060 Profile Picture
    Vipin7060 30 on at
    RE: Setting Field To Required in Different Tab through Javascript

    Hi,

    I am adding one field in two tabs , I am adding show hide javascript but that javascript is only working for first tab field only , if there is any way to solve this problem please help me 

    Thanks

  • jaytek Profile Picture
    jaytek 10 on at
    RE: Setting Field To Required in Different Tab through Javascript

    Any updates by chance?

  • hdaniel1 Profile Picture
    hdaniel1 15 on at
    RE: Setting Field To Required in Different Tab through Javascript

    I just wanted to follow up in that I'm seeing the exact same behavior with Business Rules. I have a support ticket with MS open, so hopefully can shed some light on this soon.

  • jaytek Profile Picture
    jaytek 10 on at
    RE: Setting Field To Required in Different Tab through Javascript

    I've been having this issue also. Most likely due to the update since what I had before, it did everything correctly but now it's behaving strange between tabs.

  • LeoAlt Profile Picture
    LeoAlt 16,331 on at
    RE: Setting Field To Required in Different Tab through Javascript

    Hi Partner,

    1.Business rule is also available in creating form, not only the existing records.

    2.You could try to clear your cache or try with another browser.

    I'm sorry for the inconvenience and you could raise a ticket to Microsoft about this issue.

    Regards,

    Leo

  • hdaniel1 Profile Picture
    hdaniel1 15 on at
    RE: Setting Field To Required in Different Tab through Javascript

    Thanks Leo.


    I was under the impression that Business Rules only come into play for existing records - this has to be in place prior to creating the record. 

    I am still not able to get this to work - everything works as expected in terms of saving. I am noticing now that the red asterisk appears / disappears only after I start editing the field based on it's actual requirement level.

    So technically, the code is doing exactly what I want it to do, but the UI is not updating to reflect that, which is a poor user experience.

  • Suggested answer
    LeoAlt Profile Picture
    LeoAlt 16,331 on at
    RE: Setting Field To Required in Different Tab through Javascript

    Hi Partner,

    Yes, I tried to switch to my other tab and the code still worked.

    You should pay attention that if you set value to your "fieldtocheck" first, and then your "fieldtoupdateasrequiredornot" will be business required right? Then you switch to the other tab and go back update the option set value to null, then the "fieldtoupdateasrequiredornot" field UI may be still business required, but if you continue to save the form without filling values into the "fieldtoupdateasrequiredornot" field, the form will be saved successfully which means the "fieldtoupdateasrequiredornot" filed has been changed to "none" for its required level.

    Below are my test.

    1.I selected "student" in my option first time and I switch to another tab, the test field should be required by logic.

    pastedimage1603156887972v1.png

    pastedimage1603156947910v3.png

    It worked well, and then I update my option to "others", and I switch to another tab again, we can see that the required level is none and I didn't save the form during the different actions.

    pastedimage1603156980389v4.png

    pastedimage1603156997495v5.png

    BTW, since you are triggering the code on onChange event, you could use Business Rules instead, it will also be triggered when updating the field and onloading the form.

    Regards,

    Leo

  • hdaniel1 Profile Picture
    hdaniel1 15 on at
    RE: Setting Field To Required in Different Tab through Javascript

    Hi Leo,

    Thanks for your reply.

    1. I was going to try an OnSave - it may work. The problem is that the user experience is not good. Fields should be indicated as required BEFORE they save, as opposed to after.

    2. My code is fine - it works as expected UNLESS you switch tabs. Did you try switching your tabs and making sure the field was still updating after toggling with the OnChange?

    For reference, this is my code for on change:

    fieldTypeOnChange = function (executionContext) {
    var frmCtx = executionContext.getFormContext();
    var attr = frmCtx.getAttribute("fieldtoupdateasrequiredornot");
    var fieldToCheck = frmCtx.getAttribute("fieldtocheck");
    if (fieldtocheck.getValue() !== null) {
    attr.setRequiredLevel("required");
    } else {
    attr.setRequiredLevel("none");
    }
    };

  • Verified answer
    LeoAlt Profile Picture
    LeoAlt 16,331 on at
    RE: Setting Field To Required in Different Tab through Javascript

    Hi Partner,

    1.I add the JS code on the onSave event of the form, and I set my test field business required level on "optional" in default. So when the option set value met the requirement, then  after I saved the form, the onSave event triggered and the test field changed to Business Required.

    pastedimage1603072556603v1.png     pastedimage1603072570826v2.png

    And if I toggled to "Test Tab" where my test field on, and then go back to "General" tab and change the option set value and save the form, the test field required level also changed.

    pastedimage1603072690477v3.png  pastedimage1603072702915v4.png

    Since we are triggering the JS code on onSave event, the JS will be triggered after we click the save button on form, if we only change the option set value and don't save the form, the JS won't be triggered.

    2.I add the JS on option set field onChange envet.

    pastedimage1603072946861v5.png

    Pay attention that if you are using Onchange event, you must add complete IF&Else code.

    function setRequire(executionContext){
        var formContext=executionContext.getFormContext();
        var selectedValue=formContext.getAttribute("new_type").getValue();
        if(selectedValue==1){
            formContext.getAttribute("new_testfield").setRequiredLevel("required");
        }
        else{
            formContext.getAttribute("new_testfield").setRequiredLevel("none");
        }
    }
    If we don't add the "else" condition to set the field required level back to "none", if we change the option to other values, there will be nothing happened.
    Regards,
    Leo

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,433 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans