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");

 

4 Responses to JavaScript: Get a value from a parent form

  1. Aad says:

    Hi,

    Is there a way to use similar code to get a value from the child entity and enter it into the parent entity?

    Many thanks

    Aad

  2. Peter says:

    Good question Aad, I'm curious about that one too.

  3. mitch says:

    Not easily because You can never tell when the child form is open. That means you'll have to go to the database.

    Take a look at the Ascentium JavaScript Library:

    http://consulting.ascentium.com/blog/crm/Post129.aspx

    It will save you tons of time.

    Mitch

  4. Elliot says:

    I found this script incredibly useful thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>