Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Event Handler - Reset Field

(0) ShareShare
ReportReport
Posted on by 99

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
    99 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
    13,212 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
    99 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
    99 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
    99 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
    13,212 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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,002 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,852 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans