Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Marketingpage OnChange event lookup Marketingform

Posted on by 34

Hi all,

I'm trying to create a script for the scenario in which when a lookup field on a marketingform (hosted on D365 MPage) is set an OnChange kind of script should trigger with a validation.
After trying myself I keep getting stuck on the fact that the script won't trigger on a select (however it will upon manual input).

As I am pretty new to the coding side can anybody give me some direction?

<script>
MsCrmMkt.MsCrmFormLoader.on('afterFormLoad', function (event) {

const Course = document.getElementById('79f74c1e-1c0f-ed11-82e4-000d3a2dc796');
Course.addEventListener('oninput', (event) => {});

oninput = (event) => {

if (event.target.value.includes ('XYZ')) {
//enable checkbox
document.getElementById('705944c9-f8c7-ec11-a7b5-000d3a233ba7').disabled = false;
}
else {
//disable checkbox
document.getElementById('705944c9-f8c7-ec11-a7b5-000d3a233ba7').disabled = true;
}
};
});
</script>

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Marketingpage OnChange event lookup Marketingform

    Hi Stevie_nld,

    Sorry for late replying. I tested this on my side. The onchange event would not be triggered too. The reason for this is that when you select another option in the Lookup dropdown list, the input's value would not be changed, it changes the 'data-value' property of the input:

    pastedimage1659943530628v1.png

    So you would monitor the change of this property, here is my test code, you could have a try:

    <script>
    MsCrmMkt.MsCrmFormLoader.on('afterFormLoad', function (event) {

    const Course = document.getElementById('58654b56-e216-ed11-b83e-000d3a5bc6b1');
    console.log(Course);
    var observer = new MutationObserver(function(mutations) {
    console.log('data-value changed');
    //your code
    });
    observer.observe(Course, {
    attributes: true,
    attributeFilter: ['data-value'] });
    });
    </script>
    Result:
    Onchange.gif
    BTW, 'blur' can be a workaround. However, it's not perfect. Because it would be trigger when you select the same value twice.
  • Stevie_nld Profile Picture
    Stevie_nld 34 on at
    RE: Marketingpage OnChange event lookup Marketingform

    I found a working solution....not sure if it's a nice one but hey....

    I used blur and not change, and that did the trick.

    So

    Course.addEventListener('blur', (event) => {});

  • Stevie_nld Profile Picture
    Stevie_nld 34 on at
    RE: Marketingpage OnChange event lookup Marketingform

    Hi Steve Zhao,

    Thanks for your reply!

    When using the OnChange, it does not seem to register all changes to the field.

    When I select a value, the script is not triggered (tested by adding an alert).

    Only after I add a <space> or whatever in the field after selecting it will trigger.

    Any idea how to fix this?

    Kinds Regards,

    Stevie

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Marketingpage OnChange event lookup Marketingform

    Hi Stevie_nld,

    'oninput' event occurs when an element gets user input. Please try to change your event to 'onchange' to have a try.

    It should trigger after you change the lookup value.

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