Skip to main content

Notifications

Announcements

No record found.

Customer Service forum

window.addEventListener is not working in Dynamics 365

(0) ShareShare
ReportReport
Posted on by 135

Hi Experts,

Recently we have upgraded from MS CRM 2016 to Dynamics 365 and some JavaScript functionality is not working, we have web resource and loading it onload of the form but it is not working in Dynamics 365 and working fine in MS CRM 2016. It would be great if you can suggest on this. 

JavaScript OnLoad

var FieldEvent = document.createEvent('Event');
FieldEvent.initEvent('TestCall', true, false);

HTML Web Resource

 window.addEventListener('TestCall', function (e) {
           //logic

       });

Thanks

Srikanth

Categories:
  • Suggested answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: window.addEventListener is not working in Dynamics 365

    Hi partner,

    I tried in my instance and I found it how to trigger the web resource funciton in form events.

    1.Add your html web resource into the form.

    pastedimage1570173268607v1.png

    2.Get this web resource by code in form event.

    Here is the code in form event.

    function callchild(executionContext) {
        var formContext = executionContext.getFormContext();
        formContext.getControl("WebResource_BPFPage").getObject().contentWindow.window.childFun();
    }

    Pay attention that we should use "WebResource_BPFPage" as name in "getControl" function not web resource name.

    Here is the code in html web resource. We could see that there is a "childFun" in it.

    <!DOCTYPE html>
    <html>

    <head>
        <script>
           
            function childFun(){
                alert("childFunction!");
            }
        </script>
    </head>

    <body>

        <h1>A Web Page</h1>
        

    </body>

    </html>

    And here's the result.

    pastedimage1570173446504v3.png

    Hope it helps.

    Best Regards,

    Leo

  • Srikanth Reddy Profile Picture
    Srikanth Reddy 135 on at
    RE: window.addEventListener is not working in Dynamics 365

    Hi Leo,

    Xrm.Page.getControl("WebResource_TestCustomCombo").getObject().contentWindow.window.mytest(); is also not working

    Is there any alternate way to call the HTML Web Resource function from Form JavaScript in Dynamics 365.

  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: window.addEventListener is not working in Dynamics 365

    Hi partner,

    If you want to call a js function from html web resource in form event, you could refer to the following posts.

    www.briteglobal.com/.../

    www.develop1.net/.../Calling-a-function-in-a-Html-Webresource-from-another-Javascript-Webresource

    BTW, could you describe what's the error message did you have when you run the DOM in d365? Why do you need to trigger the js function through "TestCall"?

    May be we could find another way supported by D365.

    Best Regards,

    Leo

  • Srikanth Reddy Profile Picture
    Srikanth Reddy 135 on at
    RE: window.addEventListener is not working in Dynamics 365

    Leo, Thanks for your response, actually issues here is we have custom .html web resource for combo box which will get the items from the other entity. For each drop down (26 combo box) we are adding web resource and loading values into combo box this functionality is working in MS CRM 2016 but this is breaking in Dynamic 365.

    For example below is JavaScript code which we are calling from the form onload and the event listener added in the .html web resource. As we know DOM object is not supported for D365 forms.

    Could you please suggest how can we achieve it in D365.

    Form JavaScript OnLoad

    var FieldEvent = document.createEvent('Event');

    FieldEvent.initEvent('TestCall', true, false);

    HTML Web Resource

    window.addEventListener('TestCall', function (e) {

              //logic

          });

  • Srikanth Reddy Profile Picture
    Srikanth Reddy 135 on at
    RE: window.addEventListener is not working in Dynamics 365

    In your scenario how can we call JavaScript function exist in html web resource from the form JavaScript onload without using DOM object, For above code how can we handle if we move the code to the html. Please suggest 

  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: window.addEventListener is not working in Dynamics 365

    Hi partner,

    Unfortunately, D365 does not support DOM operations now.

    Do you want to add some listener to a field? If so, you could add your js action code to field "OnChange" event provided by D365.

    pastedimage1569897945854v1.png

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/events-forms-grids

    If you really want to use DOM in form, you could refer to this post.

    https://community.dynamics.com/crm/f/microsoft-dynamics-crm-forum/257501/programmatically-register-on-change-event-handler-for-a-form-in-dynamics-crm

    Else if you have more other reuqirements, please kindly share your needs to us.

    Hope it helps.

    Best Regards,

    Leo

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: window.addEventListener is not working in Dynamics 365

    The code is unsupported if you use it in Dynamics 365 forms. If you move this to your HTML then this should work.

  • Srikanth Reddy Profile Picture
    Srikanth Reddy 135 on at
    RE: window.addEventListener is not working in Dynamics 365

    Hi Ravi,

    Thanks for your response, Is there any way to replace the above DOM objects with supported code as we are using Dynamic 365 On-Premise (9.0).

    Thanks

    Srikanth Singapuram

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: window.addEventListener is not working in Dynamics 365

    Hi,

    Accessing DOM is not supported by DYnamics 365 which is why it has stopped working-

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/customize/customizations-supported#what-kinds-of-customizations-arent-supported-with-dynamics-365-for-customer-engagement-apps

    From your example, it looks like you need this event in your HTML web resource, if this is true, you can add javascript directly to you HTML web resource.

    Hope this helps.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,409 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,479 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans