web
You’re offline. This is a read only version of the page.
close
Skip to main content
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?

I have the same question (0)
  • 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,216 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,216 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Sahan Hasitha Profile Picture

Sahan Hasitha 394

#2
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 123

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 106 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans