Reputation: 69
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:
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
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