Skip to main content

Notifications

Announcements

No record found.

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

Need to make a date field required if a toggle field changes from yes to no (but not from no to yes)

Posted on by 5

Need to make a date field on a form business required only if a toggle has been changed from yes to no.

Cant use a business rule because that would make the date field mandatory even if the toggle wasn't changed (it was already on no)

Please help

  • Suggested answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: Need to make a date field required if a toggle field changes from yes to no (but not from no to yes)

    Either you can use similar to my snippet without function name because it is inline function or you can follow this: carldesouza.com/.../

  • MortySmith Profile Picture
    MortySmith 5 on at
    RE: Need to make a date field required if a toggle field changes from yes to no (but not from no to yes)

    I am implementing it now, when adding event handler what shall I type in function field

  • Suggested answer
    Amit Katariya007 Profile Picture
    Amit Katariya007 8,530 Super User 2024 Season 1 on at
    RE: Need to make a date field required if a toggle field changes from yes to no (but not from no to yes)

    Hello Morty,

    As Abdul suggested the given BR will work. Please refer below BR where I have used date field as per your Requirement.

    pastedimage1673438284871v1.png

    pastedimage1673438317267v2.png

    pastedimage1673438338387v3.png

    Thank You,

    Amit Katariya

  • Suggested answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: Need to make a date field required if a toggle field changes from yes to no (but not from no to yes)

    Have you tried using JS? That will work in your scenario

  • MortySm1th Profile Picture
    MortySm1th 5 on at
    RE: Need to make a date field required if a toggle field changes from yes to no (but not from no to yes)

    This doesn't meet the criteria because it will keep the date field mandatory even if the toggle was already at No

  • Suggested answer
    Abdul Wahab Profile Picture
    Abdul Wahab 12,070 Super User 2024 Season 1 on at
    RE: Need to make a date field required if a toggle field changes from yes to no (but not from no to yes)

    Hi MortySmith,

    A business rule can do it for you. 

    pastedimage1673403354205v1.png

  • Suggested answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: Need to make a date field required if a toggle field changes from yes to no (but not from no to yes)

    Hi MortySmith,

    You can use JavaScript to make a date field required if a toggle field changes from "Yes" to "No", but not from "No" to "Yes". Here's an example of how you can do this:

    // Get references to the toggle and date fields
    var toggleField = formContext.getAttribute("togglefieldname");
    var dateField = formContext.getAttribute("datefieldname");
    
    // Set the date field as not required initially
    dateField.setRequiredLevel("none");
    
    // Add an event listener to the toggle field
    toggleField.addOnChange(function(){
        // Check if the toggle field has been changed to "No"
        if (toggleField.getValue() === false) {
            // Make the date field required
            dateField.setRequiredLevel("required");
        } else {
            // If toggle field is not "No", set the date field back to not required
            dateField.setRequiredLevel("none");
        }
    });

    In this example, the script first gets references to the toggle and date fields by calling the formContext.getAttribute method and passing in the names of the fields. Then, it sets the date field as not required initially by calling the setRequiredLevel method and passing in the value "none".

    Next, it adds an event listener to the toggle field by calling the addOnChange method on the toggle field. This listener will be triggered whenever the toggle field value is changed. Inside the listener, the script checks if the toggle field has been changed to "No" by calling the getValue method and comparing it with false. If the toggle field is "No", the script makes the date field required by calling the setRequiredLevel

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans