Jeffrey Orris
Jeffrey Orris

Reputation: 69

Multiple postMessage to eventListener to iFrame from CRM onLoad

A bit new to javascript. I have embedded an html web resource in an iFrame in a Dynamics CRM entity. I can easily grab the input from the iFrame to populate CRM, but am having issues posting back the data from CRM to the iFrame. I can get 1 or 2 attributes to populate, but seems that anymore than that I can't (unless I step through the debugger then works for all). There seems to be some timing issue or something. I am trying to populate 28 fields in total but only show a few in the code below as an example. I used the following reference to help me do what I'm doing:

https://community.dynamics.com/enterprise/b/crmmemories/archive/2017/05/08/post-data-from-a-dynamics-crm-form-to-an-iframe

function formLoad()
        {
            var iFrame = Xrm.Page.ui.controls.get('IFRAME_name').getObject();

            var a = Xrm.Page.getAttribute("a").getValue();
            iFrame.contentWindow.postMessage({ 'a': a }, '*');

            var b = Xrm.Page.getAttribute("b").getValue();
            iFrame.contentWindow.postMessage({ 'b': b }, '*');

            var c= Xrm.Page.getAttribute("c").getValue();
            iFrame.contentWindow.postMessage({ 'c': c }, '*');

    //etc.......
        }

        <script>
            $(document).ready(function(){


                $(window).on('message', function (event) {

                        // Important. Only accept messages from trusted origins.
                        if(~event.originalEvent.origin.indexOf('https://URL')){
                            var messageData = event.originalEvent.data;
                            if (messageData.a)
                            document.getElementById("a").value = messageData.a;
                            if (messageData.b)
                                document.getElementById("b").value = messageData.b;
                            if (messageData.c)
                                document.getElementById("c").value = messageData.c;
                             //.......etc
    else{
                            return;
                        }
                    })

           })
</script>

Again, this works if I am stepping through the IE debugger which makes me think there is some timing issue. Am I approaching this the wrong way? Is there something I should be adding for multiple posts or to handle multiple posts at one time? Any extra eyes would help. Thanks.

Upvotes: 0

Views: 527

Answers (1)

Muhammed Saad
Muhammed Saad

Reputation: 131

this could be because of your form onLoad function is invoked before the iframe is loaded so the event listener is yet not attached, but if you put a debugger in the iframe javascript main function you will see it will be hit after CRM form onLoad -any other function not the one you call from the CRM- function is Executed. i think it works in debugging as the browser forces all files to be fully loaded to allow the debugging and then the event listener is attached , try to attach the event from the crm onload function or at least make sure that the iframe is fully loaded

Upvotes: 1

Related Questions