Accessing properties of the parent form in MSCRM 4.0

March 18, 2009 at 4:20 pm | Posted in Microsoft Dynamics CRM 4.0 | Leave a comment

Usually, if access to fields of a parent form is needed, field mappings are used. These are great but they only work when the child form is created. They also require that all needed fields are added to the child form, whether you need to store them or not.

Here is the scenario. You have opened an account and switched to the contacts view. Whenever you create a new contact using the “New” button, field mappings are in effect and the new contact is initialized with the proper field values of the account.

But what happens when opening “Mary Beth Bergstrom”, the existing contact in the grid? Well, as all of you know, field mappings do only work on creation, so nothing happens – except “Mary Beth Bergstrom” is opened. If you want to access the account fields from the client side script executed in the opened (new or existing) contact form, try the following code in your OnLoad event:

if (
    (window.opener != 
null) && 
    (window.opener.parent != 
null) && 
    (window.opener.parent.document != 
null) && 
    (window.opener.parent.document.crmForm != 
null)) {

    var parentForm = window.opener.parent.document.crmForm;
    var
 formType;

    switch(parentForm.FormType) {

        case 1:
            formType = 
“Create Form”;
            
break;

        case 2:
            formType = 
“Update Form”;
            
break;

        case 3:
            formType = 
“Read Only Form”;
            
break;

        case 4:
            formType = 
“Disabled Form”;
            
break;

        case 5:
            formType = 
“Quick Create Form”;
            
break;

        case 6:
            formType = 
“Bulk Edit Form”;
            
break;

        default:
            formType = 
“Undefined Form Type”;
            
break;
    }

    var msg = “Form type = “ + formType + 
        
“\r\nObject Type Name = “ + parentForm.ObjectTypeName + 
        
“\r\nObject Type Code = “ + parentForm.ObjectTypeCode +
        
“\r\nObject Id = “ + parentForm.ObjectId;

    alert(msg);

    if (parentForm.ObjectTypeName == “account”) {
        msg = 
“Account name = “ + parentForm.all.name.DataValue +
            
“\r\nCustomer Type = “ + parentForm.all.customertypecode.SelectedText;

        alert(msg);
    }
}

Adding this code to the OnLoad event of the contact form displays the following message box:

If the opening form is an account, a second message box is displayed:


These values are retrieved directly from the account form. The following is the same code as above, without the messages but with some comments. You can use it as a template in your own form.

if (
    //window.opener will be null in quick create forms. In general it is null if the current form 
    //running this script is displayed in a modal dialog. It is set to the opening window only, 
    //if a non-modal window is used. 

    (window.opener != 
null) && 

    //window.opener does not point to the account form. Instead it is the window hosting the 
    //contact grid. This grid is placed in an IFRAME, so we need to access the parent to get 
    //to the account form.

    (window.opener.parent != 
null) && 

    //window.opener.parent.document is the account document
    (window.opener.parent.document != 
null) && 

    //and finally window.opener.parent.document.crmForm is the account form. 
    //It may be any other form as well, so we need to check the object type name.

    (window.opener.parent.document.crmForm != 
null)) {

        //For easier access, store the form in a temporary variable
        
var parentForm = window.opener.parent.document.crmForm;

        //If the parent form displays an account
        
if (parentForm.ObjectTypeName == “account”) {
            
//everything you need to do if the parent is an account
        }

        //If the parent form displays an opportunity
        
else if (parentForm.ObjectTypeName ==“opportunity”) {
            
//everything you need to do if the parent is an opportunity
        }

        …
}

 

Hope it helps!

Advertisements

Leave a Comment »

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: