Home

Jan 5, 2012

Custom Lookup view for related entity records

I was working on requirement where accounts and contacts having n:n relationship. N:N related contacts are being displayed company contacts. There is also active contact lookup field on account form i.e. account 1:n contact related.

I wanted to display only contacts those are related to current account in lookup view. Here is how I achieved this.

function customLookview() {
// generate new guid and specify here
var viewId = "{7595F4DA-6340-492A-BBA8-0E87A7CC532C}";

// Set the entity name
var entityName = "contact";
//Associated company contacts
var viewDisplayName = "Associated Company Contacts";
var accountId = Xrm.Page.data.entity.getId();
//fetch xml to get related contacts
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='contact'>" +
"<attribute name='fullname' />" +
"<attribute name='telephone1' />" +
"<attribute name='emailaddress1' />" +
"<attribute name='jobtitle' />" +
"<attribute name='fax' />" +
"<attribute name='address1_postalcode' />" +
"<attribute name='address1_line1' />" +
"<attribute name='address1_stateorprovince' />" +
"<attribute name='address1_country' />" +
"<attribute name='address1_city' />" +
"<attribute name='mobilephone' />" +
"<attribute name='contactid' />" +
"<order attribute='fullname' descending='false' />" +
"<link-entity name='new_contact_account' from='contactid' to='contactid' alias='aa'>" +
"<filter type='and'>" +
"<condition attribute='accountid' operator='eq' value='" + accountId + "' />" +
"</filter>" +
"</link-entity>" +
"</entity>" +
"</fetch>";
//grid xml for formatting view
var layoutXml = "<grid name='resultset' object='2' jump='fullname' select='1' icon='1' preview='1'>" +
"<row name='result' id='contactid'>" +
"<cell name='fullname' width='300' />" +
"<cell name='emailaddress1' width='150' />" +
"<cell name='parentcustomerid' width='150' />" +
"<cell name='telephone1' width='125' />" +
"</row>" +
"</grid>";
// specify the schemaname of the lookup control
var lookupControl = Xrm.Page.ui.controls.get('new_coborrowerid');
// set the parameters
lookupControl.addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
}
Thanks to Nishant for this post - 
http://nishantrana.wordpress.com/2010/12/31/filtered-lookup-in-crm-2011/
Happy Coding,

No comments: