Show a Modal Dialog and return a lookup

The following code snippet shows you how to return a lookup from a modal dialog.
I used this code to replace a standard CRM 4 Lookup and display my own custom made lookup screen. When clicking OK in this custom screen a lookup should be returned with the Entity the user has selected in the Custom Lookup.


Place this function in the CRM Form. 

document.CustomLookup = function ()
  var url = “/ISV/<yourapp>/CustomLookup.aspx?orgname=” + ORG_UNIQUE_NAME;
        var selectedItems = window.showModalDialog(url, null, ‘dialogWidth:600px;dialogHeight:400px;resizable:yes’);                    
        if (selectedItems != null)

            crmForm.all.myField.DataValue = selectedItems ; 
            crmForm.all. myField.FireOnChange();    // If you also need to fire the onchange event

Then use this function as the onclick event of the Lookup.

    crmForm.all.mylookup.onclick = document. CustomLookup;
} catch (e) {}

In the CustomLookup.aspx page add a Gridview that is populated with the entities you need. And add a JavaScript function that will retrieve the selected item in the grid. (I usually work with a SelectedRow style)

<script type=”text/javascript”>    
        function applychanges()
            var Entities = document.getElementById(‘<%= ComponentsGrid.ClientID %>’).getElementsByTagName(“TR”);  // ComponentsGrid is the name of the gridview            
            var EntityID = “”;
            var EntityName = “”;
            for (var i=1;i< Entities .length;i++)
                if (Entities [i].className == “SelectedRow”)
                    EntityID  = Entities [i].getAttribute(“oid”);         
                    EntityName  = Entities [i].getAttribute(“oname”);                            

            var lookup = new Array();
            lookup[0] = new LookupControlItem();
            lookup[0].id = EntityID  ;
            lookup[0].typename = “myentityname”;
            lookup[0].name= EntityName  ;
            window.returnValue = lookup;

Tagged with: ,
Posted in CRM 4

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: