06 May 2009

CRM 4.0 IFrame: Show Entity's Associated View

It's a common requirement to show entity's associated view(1:N, N:N) in IFrame, the below code works for both 1:N and N:N relationship, it also works on both On-Premise and IFD deployment. All you need to do is find out(IE Developer Toolbar) the ID of the associated link.

The 1:N relationship needs these parameters in the request URL: oId, oType, security, tabSet
The N:N relationship needs an extra parameter: roleOrd in the request URL, which has been involved in the code.



var navId = "nav_new_new_myentity_account";

if(document.getElementById(navId) != null)
{
var tmp = document.getElementById(navId).onclick.toString();
tmp = tmp.substring(tmp.indexOf("'")+1, tmp.indexOf(";"));
var loadArea = tmp.substring(0, tmp.indexOf("'"));
var roleOrd = (tmp.indexOf("roleOrd") == -1) ? -1 : tmp.substring( tmp.indexOf("roleOrd"), tmp.lastIndexOf("'")).replace("\\x3d", "=");
crmForm.all.IFRAME_view.src = (roleOrd == -1) ? GetFrameSrc(loadArea) : GetFrameSrc(loadArea) + "&" + roleOrd;

}

function GetFrameSrc(tabSet)
{
if (crmForm.ObjectId != null)
{
var id = crmForm.ObjectId;
var type = crmForm.ObjectTypeCode;
var security = crmFormSubmit.crmFormSubmitSecurity.value;
var path = document.location.pathname.substring(0, document.location.pathname.indexOf("edit.aspx")) + "areas.aspx?";

return (path + "oId=" + id + "&oType=" + type + "&security=" + security + "&tabSet=" + tabSet);
}
else
{
return "about:blank";
}
}



Enjoy it! ;-)

25 comments:

  1. Anonymous11:38 am

    Hi Jim

    Good post - just wondered is there a way to remove the white border that surrounds the view, to make the form look a bit more seamless. Or resize the view to fill the entire IFRAME?

    Thanks SB

    ReplyDelete
  2. Hi SB,

    After much trial and error I have managed to remove the white border from my associated view. I added the following code to the onLoad() function. Obviously replace 'training_package_notes' with your iFrame name:

    //Remove the white border from the iFrame
    var frameName = 'IFRAME_training_package_notes';
    var objFrame = document.getElementById(frameName);
    objFrame.allowTransparency=true;

    ReplyDelete
  3. Anonymous9:30 am

    This comment has been removed by the author.

    ReplyDelete
  4. Anonymous9:32 am

    Hi Leigh

    Thanks for your help. I managed to hack and slash a solution for my immediate needs using the following code.

    //Hide borders of IFRAMES
    var frameName = 'IFRAME_XXX';
    var objFrame = document.getElementById(frameName);
    var objWindow = document.frames[frameName];
    objFrame.allowTransparency=true;
    objFrame.onreadystatechange = function ()
    {
    if (objWindow.document.readyState=='complete')
    {
    objWindow.document.body.style.backgroundColor='transparent';
    if (objWindow.document.getElementsByTagName('BODY')[0]!=null)
    {
    objWindow.document.getElementsByTagName('BODY')[0].style.padding='0px';
    }
    if (objWindow.document.getElementsByTagName('TABLE')[0]!=null)
    {
    objWindow.document.getElementsByTagName('TABLE')[0].cellPadding='0';
    objWindow.document.getElementsByTagName('TABLE')[0].style.border='0px';
    }
    }
    }

    Thanks SB

    ReplyDelete
  5. Why do you need the additional onreadystatechange stuff?

    ReplyDelete
  6. Hi Leigh

    Credit here goes to:

    http://crm.atechnisch.nl/2008/01/related-entity-in-iframe/

    I'm not sure what the code is doing in its entirety.

    Cheers

    ReplyDelete
  7. To Stringer: nice to know it works for you! ;-)

    ReplyDelete
  8. To Leigh and Inderpal: Without using onreadystatechange event, you won't know when the document has been loaded in the iframe, so it's not possible to control the element inside the iframe.

    ReplyDelete
  9. Anonymous12:22 am

    Onclick event is causing an error. What am I supposed to be entering for what the navId equals?

    all i changed was navId
    Iframe_name.src, id and type

    ReplyDelete
  10. Hi Jim,

    I used your coding. But i cannot get the result. the IF loop cannot be executed. What can i do now?

    Thanks and Regards,
    Venkatesh.S

    ReplyDelete
  11. Anonymous10:43 am

    You maked clear points about online business ideas through your blog and it is very useful to make money from online at home in part time jobs Thanks.




    How to create Blog



    How to Make Money Online



    Affiliate Programs

    ReplyDelete
  12. Thanks for posting this. My apologies but I am not understanding one thing about the code. Where in your sample do you point to the specific iframe? I'm assuming that somewhere in your code I need to rename the iframe.

    Thanks!

    ReplyDelete
  13. Jim,
    Can you please tell me how to get the id from the selected row in the grid that is displayed in an Iframe? I got this far:

    var frameDoc = document.getElementById("IFRAME_Saw").contentWindow.document;
    var a = frameDoc.all['crmGrid'].InnerGrid.SelectedRecords;
    var selectedItems = new Array(a.length);
    for (var i=0; i < a.length; i++)
    {
    selectedItems[i] = a[i][0];
    }

    That gets me the selected item, but I need the guid id of the entity to pass to reporting services

    thanks

    ReplyDelete
  14. Hi Jim,

    what if i wanna show the associated view of N:1 relationship? is it possible to do it?

    Thanks,
    DD

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. Adi, I am having issues with this working, here is what I have? Am I setting the field correctly.

    How do I use this for multiple related entities?

    var navId = "nav_new_contact_priorityissues";

    if(document.getElementById(navId) != null)
    {
    var tmp = document.getElementById(navId).onclick.toString();
    tmp = tmp.substring(tmp.indexOf("'")+1, tmp.indexOf(";"));
    var loadArea = tmp.substring(0, tmp.indexOf("'"));
    var roleOrd = (tmp.indexOf("roleOrd") == -1) ? -1 : tmp.substring( tmp.indexOf("roleOrd"), tmp.lastIndexOf("'")).replace("\\x3d", "=");
    crmForm.all.IFRAME_priorityissues.src = (roleOrd == -1) ? GetFrameSrc(loadArea) : GetFrameSrc(loadArea) + "&" + roleOrd;

    }

    function GetFrameSrc(tabSet)
    {
    if (crmForm.ObjectId != null)
    {
    var id = crmForm.ObjectId;
    var type = crmForm.ObjectTypeCode;
    var security = crmFormSubmit.crmFormSubmitSecurity.value;
    var path = document.location.pathname.substring(0, document.location.pathname.indexOf("edit.aspx")) + "areas.aspx?";

    return (path + "oId=" + id + "&oType=" + type + "&security=" + security + "&tabSet=" + tabSet);
    }
    else
    {
    return "about:blank";
    }
    }

    ReplyDelete
  17. Anonymous3:43 pm

    Hi Jim,

    First off, Great posts! I used your code for the Associated views in an Iframe with great success! (until recently)

    Everything was working great for about 99% of our users until about 3 weeks ago. Now every page that has this code running on it, freezes IE for about 20-30 seconds between clicks (sometimes more). The CPU spikes to about 50% while its frozen, nothing shows in a fiddler trace... really odd. Any thoughts? I've tried turning everything I can think of off, with no luck. Will have to look for another alternative if I can't get this corrected soon.

    Any assistance would be greatly appreciated! Thanks!
    Keith

    ReplyDelete
  18. Hi Jim,

    Thank's for the code it works great. However, when I access the parent record using IFD, the iFrame has an error: "Navigation to the webpage was cancelled." Do you know what may be going on?

    ReplyDelete
  19. Hello,

    What would be the link (IE Developer Toolbar) identification of the link?
    este código é um javascript, você adiciona ele como um recurso web?

    thanks

    ReplyDelete
  20. I beleive we are missing "}".

    ReplyDelete
  21. Hi Jim,

    I have created a custom entity called 'Stakeholder' which is related to company. I want to show the associated view of stakeholder in a tab on the company form.

    I have looked at countless websites but it doesnt see to work, is there a problem with the code I have? or is it not possible with custom entities?

    This is the code that doesnt appear to work:

    function GetFrameSource(tabSet) {
    if (crmForm.ObjectId != null) {

    var oId = crmForm.ObjectId;
    var oType = crmForm.ObjectTypeCode;
    var security = crmFormSubmit.crmFormSubmitSecurity.value;

    return "areas.aspx?oId=" + oId + "&oType=" + oType + "&security=" + security + "&tabSet=" + tabSet;
    }

    else {
    return "about:blank";
    }
    }

    crmForm.all.IFRAME_Stakeholder.src = GetFrameSource("areaStakeholder");

    If i were to use you code, and do not have the developer toolbar, how else could i find associated link, is that the ID line at the very top?

    ReplyDelete
  22. Keep up the fantastic piece of work, I read few blog posts on this web site and I believe that your site is real interesting and has lots of great information. ERP Software in Mumbai || System Software || CRM Software in Mumbai || MLM Software

    ReplyDelete
  23. I really appreciate spending some time to talk about that, I believe firmly regarding this and so really enjoy understanding more about this kind of subject.This is also a very good post which I really enjoyed reading. It is not everyday that I have the possibility to see something like this. CRM Software || MLM Software in Mumbai || ERP Software || System Software in Mumbai

    ReplyDelete