Skip to main content

Notifications

Dynamics 365 general forum
Answered

Event Handler - Reset Field

Posted on by 95

Hi All,

I am a couple weeks old in Model Driven apps using the CDS/Dataverse but learning quickly. I have a "Country" field and a "State/Province" field. I am trying to reset the "Province/State" field if the country field is changed.

I know this should be an "OnChange" event I am just not sure how this works and do not have any JS coding background. Can anyone direct me on how I can do this now and with future fields please?

  • mykedeabreu Profile Picture
    mykedeabreu 95 on at
    RE: Event Handler - Reset Field

    Hi Mehdi,

    This is working great for me thanks. Just one question - Can I add multiple fields? I tried adding multiple fields using comma but doesnt seem to work.

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,198 User Group Leader on at
    RE: Event Handler - Reset Field

    Hi mykedeabreu,

    Yes, you need to create your library. Generally, it is better to create one library per entity. Could you share screenshots for each of the steps you described in your last post.A screenshot of the error will help you to solve the problem. 

    Please note that you should always use the technical fields names when developing your JavaScript code.

    pastedimage1618255668640v2.png

    resetField function will clear the field address1_stateorprovince
    pastedimage1618256608056v3.png
    I'll be happy to help you implement your JS code to the end
  • mykedeabreu Profile Picture
    mykedeabreu 95 on at
    RE: Event Handler - Reset Field

    I have my "State" field visible set to false if Country is not populated. When I disable this business rule your method works. If I enable the business rule I cannot see the State field at all.

  • mykedeabreu Profile Picture
    mykedeabreu 95 on at
    RE: Event Handler - Reset Field

    Does it make a difference if the field I am resetting is a lookup field?

    I get:

    pastedimage1618255639364v1.png

    This is my Library Code:

    pastedimage1618255684577v2.png

    This is my Handler properties:

    pastedimage1618255730692v3.png

  • mykedeabreu Profile Picture
    mykedeabreu 95 on at
    RE: Event Handler - Reset Field

    Hi Mehdi - Thank you for your help on this. I just want to clarify if I did this right as it is giving me an error now on the form.

    1. Open Events Tab on Country Field
    2. Do I create a New Library or is this something that will already exist?
      1. I did not find it so I did create my own library using the exact code you provided and made no changes.
    3. Add OnChange Event Handler Function: resetField
    4. Check "Pass execution context as first parameter"
    5. "State" as that is my column name.

  • Verified answer
    meelamri Profile Picture
    meelamri 13,198 User Group Leader on at
    RE: Event Handler - Reset Field

    Hi, 

    Please refer to the code below: 

    function resetField(executionContext, fieldName){
        var formContext = executionContext.getFormContext();
        formContext.getAttribute(fieldName).setValue(null);
    }

    Event Handler: 

    pastedimage1618248981207v1.png

    Handler Properties

    pastedimage1618249016507v2.png

    Feel free to ask questions about my code. It works without any problem. 

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,524 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,493 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans