locked
Combine search criteria RRS feed

  • Question

  • In a lookup box, I would like to combine search criteria.  For example, Searching linked contacts where

    Account = 'Lake Mary, FL' and name = 'John Smith'.

    Is there some way to do this in the lookup or search box?

    Thursday, November 4, 2010 5:58 PM

Answers

  • Hello.

     

    As for me it is better to use this solution - http://mscrmfilteredlookup.codeplex.com/


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Thursday, November 4, 2010 7:15 PM
    Moderator
  • Ok, got you.

    The lookup field is actually a wrapper. If you want to capture the field value as you type, you will need to use its input control and attach an onkeypress event. Put the following Javascript code in your form's onload event:

    // Get the input control of the customerid lookup field
    var control = document.getElementById("customerid_ledit");
    control.attachEvent('onkeypress',search,false);
    
    function search()
    {
    var value = control.value; // Do your filter or query here... }
    Friday, November 5, 2010 6:17 PM

All replies

  • If you are talking about filtering a lookup dialog, it's doable with some customizations in CRM 4.0. Here are the steps:

    1. First, you will need to modify C:\inetpub\wwwroot\_controls\lookup\Lookupsingle.aspx and add the following code. (don't worry, it won't change your lookup behavior unless you send parameters). Refer to http://crm.georged.id.au/post/2008/02/16/Filtering-lookup-data-in-CRM-4.aspx

    <script runat="server">
    // Custom code for filtered lookup
    protected override void OnLoad( EventArgs e )
    {
     base.OnLoad(e);
     crmGrid.PreRender += new EventHandler( crmgrid_PreRender );
    }
    
    void crmgrid_PreRender( object sender , EventArgs e )
    {
     // 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"); 
     this._showNewButton = false;
     }
    }
    </script> 
    <br/>
    

     

     

    2. Add the following code to your form's onload event

    function SetupContactLookup()
    {
     // The field you want to lookup
     var lookup = crmForm.all.new_contactid;
     // Show or hide buttons and settings
     lookup.AddParam("ShowNewButton", "0"); 
     lookup.AddParam("ShowPropButton", "0"); 
     lookup.showproperty = false;
     lookup.lookupbrowse = 1;
     
     var searchValue = 'Mary';
     lookup.AddParam("search", "<fetch mapping='logical'><entity name='contact'>" + "<filter><condition attribute='firstname' operator='eq' value='" + searchValue + "' /></filter></entity></fetch>");
    }
    
    SetupContactLookup();
    


    You can hookup the function with any lookup field on your form and do a filtered search. To do multiple conditions, just append another <filter><condition attribute....</filter> statement inside.

    Hope that helps.
    Thursday, November 4, 2010 6:19 PM
  • Actually, I'm trying to do both.  I have some script to filter lookup dialogs already, but I can only get it to accept one search criteria:

    document.FilterLookup = function(source, target) {
        if (IsNull(source) || IsNull(target)) { return; }
        var name = IsNull(source.DataValue) ? '' : source.DataValue[0].name;
        target.additionalparams = 'search=' + name;
    }

    if (crmForm.all.customerid != null) {
      document.FilterLookup(crmForm.all.customerid, crmForm.all.responsiblecontactid);
    }

    If I try to set the 'name' to more than one value, it doesn't work.  I need to do an 'and' search on contacts like

    ParentCustomer = 'Lake Mary, FL' AND FullName = 'John Smith'.

    I can put in 'Lake Mary, FL' and it works, but I can't further filter the results by name in the lookup dialog, or in a regular quick search box.

    Thursday, November 4, 2010 6:41 PM
  • Umm ok... maybe you can give the method a try for the lookup dialog.
    It works for multiple conditions:

    <fetch mapping='logical'><entity name='contact'>" + "<filter><condition attribute='firstname' operator='eq' value='" + searchValue1 + "' /></filter><filter type='and'><condition attribute='parentcustomer' operator='eq' value='" + searchValue2 + "' /></filter></entity></fetch>
    
    Thursday, November 4, 2010 6:46 PM
  • Hello.

     

    As for me it is better to use this solution - http://mscrmfilteredlookup.codeplex.com/


    Microsoft CRM Freelancer

    My blog (english)
    Мой блог (русскоязычный)
    Thursday, November 4, 2010 7:15 PM
    Moderator
  • Ok, so here's an additional question.  How do I get the value that they type into the box so I can then do a search using this method? I get the onchange event where I can capture, but since the value for the actual field isn't set yet how do I get whatever they typed into the box?

     

    Friday, November 5, 2010 3:46 PM
  • Hi, need a little clarification regarding your question. Do you mean you want to capture what you type into a lookup field on a form? (rather than using the lookup button?)
    Friday, November 5, 2010 5:39 PM
  • Yes.  Basically if they type 'john smith' into the lookup field, I want to capture that, combine it with the current account as a secondary AND criteria, then do the fetch XML search.

    I don't see a property of the field that will show me what is currently typed in there?  If you do xxx.DataValue[0].Name it's NULL until something is actually set.

    Friday, November 5, 2010 5:52 PM
  • Ok, got you.

    The lookup field is actually a wrapper. If you want to capture the field value as you type, you will need to use its input control and attach an onkeypress event. Put the following Javascript code in your form's onload event:

    // Get the input control of the customerid lookup field
    var control = document.getElementById("customerid_ledit");
    control.attachEvent('onkeypress',search,false);
    
    function search()
    {
    var value = control.value; // Do your filter or query here... }
    Friday, November 5, 2010 6:17 PM