Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

How to Use JavaScript to Adjust Controls Like Toggles

Posted on by 15

Hi,

I have JavaScript on my form to disable a two-option field, based on the value of a lookup. Without putting the entire function here, basically I'm using:

formContext.getControl(approvalToggle).setDisabled(true);
This function runs both on load and on change of the lookup fields. My script sets the default to disabled if the lookup is empty. When a specific value is used in the lookup, then the two-option field becomes enabled.
This works just fine. The problem is that if I use the Toggle control on the field, the field can be enabled with the right lookup value, but the control itself remains disabled.
For example:
Screenshot-2020_2D00_09_2D00_22-140032.png
In the first row, the Toggle control isn't being used and the lookup has the right value to unlock the field. Everything is working fine. In the third row, the lookup is empty, the field is locked, also everything working fine. But in the second row, the lookup has the right value to unlock the field (you can see the lock icon disappears) but the Toggle control remains inoperable. If I save, then refresh the browser (chrome or edge, same issue) then the Toggle control becomes active.
So I guess controls on fields have to be referenced separately? Does anyone know how to do this?
  • Suggested answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: How to Use JavaScript to Adjust Controls Like Toggles

    Hi Sean,

    Thanks for your details, I tried as you said and it is a known issue.

    Toggle control is not recommended to use in D365 now, please use the default control instead.

    Sorry for the inconvenience :(

    Best Regards,

    Leo

  • Sean Henderson Profile Picture
    Sean Henderson 15 on at
    RE: How to Use JavaScript to Adjust Controls Like Toggles

    Hi, thanks for your reply!

    A couple things:

    1) I simplified the scenario for the purpose of highlighting the issue. In my actual solution, the custom code is doing quite a bit more heavy lifting and business rules can't even come close to handling it.

    2) The issue isn't with the ability to lock and unlock a field at the field property level, I can do that easily enough in javascript. The issue is with the Toggle control. It looks like in your example, you didn't add the control, you just have the default yes/no box. I even tested this out with a business rule and it has the same issue. It properly unlocks and locks the field property, but the Toggle control itself remains inoperable. Add the Toggle control to your field and you will see what I mean.

  • Suggested answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: How to Use JavaScript to Adjust Controls Like Toggles

    Hi Partner,

    About this requirement, I suggest you to use Business Rules instead.

    For example I created a business rule to check if the account is "Fourth Coffee" then unlock the "Director Approval"(Locked in default as yours).

    pastedimage1600914633544v7.png

    pastedimage1600914419694v3.png  pastedimage1600914432135v4.png

    And let's have a try. When loading the form with no data in Account, the fields are locked as we expected.

    pastedimage1600914397675v1.png

    Then we choose the right account, the Director Approval get unlocked.

    pastedimage1600914525929v5.png

    And if we selected other wrong account, the Director Approval get locked again.

    pastedimage1600914675723v8.png

    The business rule will always be available both in onloading the form or onchanging the field without refresh the form.

    And in your case, there may be some mistake in your logic code, to find out the reason, please share your complete code here.

    On the whole, using Business Rules to control the fields will be easier and more accurate than JS code now.

    Best 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

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans