Knowledge found and lost while working with Microsoft Dynamics CRM
RSS icon Home icon
  • JavaScript: Get a value from a parent form

    Posted on August 24th, 2009 Mitch Milam Print Print 4 comments

    It is not an uncommon requirement to pull data for from the parent entity of the current record.  In many cases, this child entity is only created from its parent so we can simply just reference the window.opener object to get a reference to the parent form.

    An example would be creating a new Contact or Order from inside the Account form.

    I got tired of writing this code over and over so yesterday I set down to attempt a solution that would be dynamic and I could just call from the Form’s OnLoad event.  Here’s my solution:

    function GetParentFormFieldValue(fieldName)
    {
        var retVar = null;
        
        if (
            (window.opener != null) &&
            (window.opener.parent != null) &&
            (window.opener.parent.document != null) &&
            (window.opener.parent.document.crmForm != null)
           )
        {
            eval("retVar = window.opener.parent.document.crmForm.all." + fieldName + ".DataValue");
        }
        
        return retVar;
    }
     

    It is used like this:

    crmForm.all.name.DataValue = "New order for " + GetParentFormFieldValue("name");

    Customization, Dynamics CRM
    1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
    Loading ... Loading ...
    1,519 views