Set Custom View as Default View for Lookup Control


Let’s say we have to set Custom View as Default View of a particular Lookup. Below are the steps to set Custom View as default View.

1. Create a Custom View as per your requirement and Save it.(In my case, Created a custom view of Account which will be shown on contact entity for Primary Customer Field)
CustomView

2. Get View Id from Browser. 3. Download Fetch XML for your custom view as shown below. In my case, below is the FetchXML query..

<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>   <entity name=”account”>     <attribute name=”name” />     <attribute name=”primarycontactid” />     <attribute name=”telephone1″ />     <attribute name=”accountid” />     <attribute name=”accountratingcode” />     <order attribute=”name” descending=”false” />     <filter type=”and”>       <condition attribute=”telephone1″ operator=”eq” value=”555-0135″ />     </filter>   </entity> </fetch>

4. Format the FetchXML  as string variable as below.

    var fetchXml = “<fetch version=’1.0′ “ +
 “output-format=’xml-platform’ “ +
 ” mapping=’logical’ “ +
 ” distinct=’false’>” +
  “<entity name=’account’>” +
    “<attribute name=’name’ />” +
    “<attribute name=’primarycontactid’ />” +
    “<attribute name=’telephone1′ />” +
    “<attribute name=’accountid’ />” +
    “<attribute name=’accountratingcode’ />” +
    “<order attribute=’name’ descending=’false’ />” +
    “<filter type=’and’>” +
     ” <condition attribute=’telephone1′ operator=’eq’ value=’555-0135′ />” +
    “</filter>” +
  “</entity>” +
“</fetch>”;
5. Create layoutXml to set Grid properties to display the view as below.
var layoutXml = “<grid name=’resultset’ “ +
“object=’1′ “ +
“jump=’name’ “ +
“select=’1′ “ +
“icon=’1′ “ +
“preview=’1′>” +
“<row name=’result’ “ +
“id=’accountid’>” +
“<cell name=’name’ “ +
“width=’300′ />” +
“<cell name=’primarycontactid’ “ +
“width=’150′ />” +
“<cell name=’telephone1′ “ +
“width=’100′ />” +
“<cell name=’accountratingcode’ “ +
“width=’100′ />” +
“disableSorting=’1′ />” +
“</row>” +
“</grid>”;

6. Call the addCustomView() method of Lookup to set custom view as Default View.

Sets the viewId, viewDisplayName, fetchXml, and layoutXml variables to pass as arguments so that a custom view is added as the default view to the control for the Parent Customer lookup field.

7. Add above steps in a custom function and call it on OnLoad or OnChange as per requirement
function setDefaultCustomView() {
    var viewId = “{00000000-0000-0000-00AA-000010001001}”;
    var viewDisplayName = “Custom Account View”;
    var fetchXml = “<fetch version=’1.0′ ” +
“output-format=’xml-platform’ ” +
” mapping=’logical’ ” +
” distinct=’false’>” +
“<entity name=’account’>” +
“<attribute name=’name’ />” +
“<attribute name=’primarycontactid’ />” +
“<attribute name=’telephone1′ />” +
“<attribute name=’accountid’ />” +
“<attribute name=’accountratingcode’ />” +
“<order attribute=’name’ descending=’false’ />” +
“<filter type=’and’>” +
” <condition attribute=’telephone1′ operator=’eq’ value=’555-0135′ />” +
“</filter>” +
“</entity>” +
“</fetch>”;

    var layoutXml = “<grid name=’resultset’ ” +
“object=’1′ ” +
“jump=’name’ ” +
“select=’1′ ” +
“icon=’1′ ” +
“preview=’1′>” +
“<row name=’result’ ” +
“id=’accountid’>” +
“<cell name=’name’ ” +
“width=’300′ />” +
“<cell name=’primarycontactid’ ” +
“width=’150′ />” +
“<cell name=’telephone1′ ” +
“width=’100′ />” +
“<cell name=’accountratingcode’ ” +
“width=’100′ />” +
“disableSorting=’1′ />” +
“</row>” +
“</grid>”;

Xrm.Page.getControl(“parentcustomerid”).addCustomView(viewId, “account”, viewDisplayName, fetchXml, layoutXml, true); 

}

Note: Pls change the double quote(“) manually before testing the code.

Filtering multiple custom CRM Lookup


Let me explain the scenario..

We have a Model lookup and a variant Lookup on Lead form. Every variant is associated with a particular model. i.e One model have multiple Variant.

Eg.
Model          Varient
Alto             Alto LX
Alto             Alto Xcite
Alto             Alto LXI
SX4               SX4 VXI
SX4               SX4 ZXI Leather
SX4               SX4 ZXI
—————————-

Now Requirement is that when user select a model from Model lookup then only variant corresponding to selected model should be displayed to user when user click on variant lookup and disable the search in Variant lookup.

Write below code on Model OnChange event.

if(crmForm.all.new_modelid.DataValue != null )
{
var modelLookup=crmForm.all.new_modelid.DataValue;
var modelID=modelLookup[0].id;
var modelName=modelLookup[0].name;
var field=crmForm.all.new_variantid;

// Pass fetch xml through search value parameter
field.lookupbrowse = 1;
field.AddParam(“search”,”<fetch mapping=’logical’ distinct=’false’>”
+”<entity name=’new_varientmaster’>”
+”<attribute name=’new_varientmasterid’/>”
+”<attribute name=’new_name’/><attribute name=’createdon’/>”
+”<order attribute=’new_name’ descending=’false’/><filter type=’and’>”
+”<condition attribute=’new_modelnameid’ operator=’eq’ uiname='”+ modelName
+”‘ uitype=’new_modelmaster’ ”
+”value='”+ modelID +”‘ />”
+”</filter></entity></fetch>”

);
}

Note: Put below code if not already exist

The following code needs to be inserted anywhere in the <CRM site folder>\_controls\lookup\lookupsingle.aspx file if not already exist (if you have already code for filtering lookup)..

<script runat=”server”>
protected override void OnLoad( EventArgs e )
{
base.OnLoad(e);
crmGrid.PreRender += new EventHandler( crmgrid_PreRender );
}

void crmgrid_PreRender( object sender , EventArgs e )
{
// As we don’t want to break any other lookups, ensure that we use workaround only if
// search parameter set to fetch xml.
if (crmGrid.Parameters[“search”] != null && crmGrid.Parameters[“search”].StartsWith(“<fetch”))
{
crmGrid.Parameters.Add(“fetchxml”, crmGrid.Parameters[“search”]);

// searchvalue needs to be removed as it’s typically set to a wildcard ‘*’
crmGrid.Parameters.Remove(“searchvalue”);

// and then select it.
this._showNewButton = false;
}
}

</script>

For details  about lookupsingle.aspx code visit below link.

http://crm.georged.id.au/post/2008/02/16/Filtering-lookup-data-in-CRM-4.aspx

For Reference: Model is custom entity (new_modelmaster) which contains name attribute (i.e Model Name) and Variant is custom entity (new_variantmaster) which contain name attribute( variant Name ) and Model lookup(new_modelnameid) to map model with variant.

For building FetchXml Query find below link.

https://arvindcsit.wordpress.com/2010/09/08/using-the-advanced-find-for-fetchxml-builder/

Enjoy J

Using the Advanced Find for FetchXML builder


You can just open the Advanced find page and build the query as you like. Then run the query to see if it returns the data as you wish it should. If you are satisfied with the results and you want to know what query was sent into the CRM Framework, then press F11 to get the address bar and enter this script and press enter.

javascript:alert(resultRender.FetchXml.value);

If you get a warning about leaving the page, just press ‘ok’ and then the query which is sent to the framework opens up in a popup. Unfortunately you cannot select the text to copy and paste. But with Windows XP and Windows Server 2003 you can copy all text on the popup by clicking somewhere on the popup (not on the button “OK” ofcourse) and pressing ctrl+c. Now in notepad you can paste the text of the FetchXML.

Good luck!

You can also try this instead of the alert:

javascript:prompt(“my query:”, resultRender.FetchXml.value);