Forums

Forums / Developing Portals / How to reload lookup field filter

How to reload lookup field filter

Thread is closed for posting
8 posts, 1 answered
  1. quanganh
    quanganh avatar
    33 posts
    Registered:
    16 Jun 2016
    16 Jun 2016
    Link to this post
    I have 2 lookup fields. I want to filter the lookup field when I change value of the other one.

    How can I do this ?

    Many thanks,
    Quang Anh
     
  2. Missing user
    Missing user avatar
  3. mbayes
    mbayes avatar
    17 posts
    Registered:
    29 Dec 2015
    16 Jun 2016
    Link to this post
    Hi Quang,

    The only way that I've been able to implement this functionality was by using the query string. There is an advanced property on a lookup field that allows you to pass the value into the query string. Once this is passed in, the form would refresh with the new value that you specify and then you could build your second lookup field FetchXML filtered to the query string value.

    I am unsure if there is a better way of doing this, and am sure that someone from TPC will hopefully respond in a matter of time. If you have any other questions feel free to post back here and I'll try to help.

    Matt
  4. quanganh
    quanganh avatar
    33 posts
    Registered:
    16 Jun 2016
    20 Jun 2016
    Link to this post
    Hi Matt,

    I found this property on a lookup field. I setup these properties
    PassValueToQuerystring = true
    QuerystringIdForPassedValue = myquerystring

    It work, but my form have a lot required validator. it cannot pass the value into the query string if the form is invalid. And the page will be reload each time we pass the value into the query string. This behaviours seems not good.

    Ideally, can we use javascript to re-filter the second lookup field ??

    Many thanks,
    Quang Anh
     
     
     
     
  5. mbayes
    mbayes avatar
    17 posts
    Registered:
    29 Dec 2015
    20 Jun 2016 in reply to quanganh
    Link to this post
    Hi Quang,

    I'm sorry to hear that although it was fixed, it wasn't what you needed.
    The only option would be to use Javascript. You can use the following code below to get a reference to the RadComboBox object:

    findControl("CrmLookup_C001").get_cboLookup()

    You will have to find the ID of your control instead of my C001 ID. Once you have that, you can reference here for a starting point.

    It seems like you'll have to use Attributes, and depending on what you want, you will have to find all items in one list that meet your criteria, then clear and re-add to the list so you only have the items you want.

    If you have any issues or are confused in any way, don't hesitate to let me know. 

    Thanks.

    Matt Bayes
  6. Clinton Bale
    Clinton Bale avatar
    126 posts
    Registered:
    21 Feb 2014
    21 Jun 2016
    Link to this post
    Hello Quang & Matt,

    Thanks for providing some clarification Matt, it has been useful.

    In regards to filtering lookups on other lookups, TPC 3.3 just released with the new "Saved Query" feature. This allows you to execute Fetch XML securely on the client-side using JavaScript. This also opens up the possibility of doing client-side "filtering lookups on other lookups". We will be providing an example of how to do this in the coming week.

    Regards,
    Clinton
  7. quanganh
    quanganh avatar
    33 posts
    Registered:
    16 Jun 2016
    26 Jun 2016
    Link to this post
    Hello Clinton,

    Could you please show me an example of how to do this?

    Many thanks,
    Quang Anh
  8. Clinton Bale
    Clinton Bale avatar
    126 posts
    Registered:
    21 Feb 2014
    Answered
    28 Jun 2016 in reply to quanganh
    Link to this post
    Hello Quang,

    I have developed a sample that works to filter contacts that have the parent custom of an account.

    First you must create a saved query that gets all contacts under the account:

    <fetch version="1.0" mapping="logical" output-format="xml-platform">
      <entity name="contact">
        <attribute name="contactid" />
        <attribute name="fullname" />
        <order attribute="fullname" descending="false" />
        <filter type="or">
          <condition attribute="parentcustomerid" operator="eq" value="@p1@" />
        </filter>
      </entity>
    </fetch>
    

    Then add the following JavaScript to the form or page. Changing the names of accntName and contactName to your respective control names. Also modify the SavedQuery webservice name to the one you just created.

    Sys.Application.add_load(function(){
      var clearCombo = function(cbo) {    
        while(cbo.get_items().get_count()>1) { // clear existing
          cbo.trackChanges();
          cbo.get_items().removeAt(cbo.get_items().get_count()-1);
          cbo.commitChanges();
        }
      }
      var accntName = "CrmLookup_C094", contactName = "CrmLookup_C015";
      var accntControl = findControl(accntName), contactControl = findControl(contactName);
      if(accntControl) {
        contactControl.disable();
        accntControl.get_cboLookup().add_selectedIndexChanged(function(sender,args){
          var item = args.get_item();
          contactControl.disable();
          if(item) {
            var accntId = item.get_value();        
            if(accntId&&accntId!="00000000-0000-0000-0000-000000000000") {
              try {
                var contacts = $.ajax("http://www.website.com/SavedQueryService/Execute/GetContactsForAccount/"+accntId,{
                  async:false,
                }).responseJSON;
                var contactCombo = contactControl.get_cboLookup();
                if(contactCombo) {
                  clearCombo(contactCombo);
                  contactCombo.trackChanges();
                  for(var i = 0; i < contacts.length; i++) { // add from api call
                    var contactItem = new Telerik.Web.UI.RadComboBoxItem();
                    contactItem.set_text(contacts[i].Attributes["fullname"]);
                    contactItem.set_value(contacts[i].Attributes["contactid"]);
                    contactCombo.get_items().add(contactItem);
                  }
                  contactCombo.commitChanges();  
                  contactControl.enable();
                }
              } catch(ex) {console.log(ex);}
            } else {
              clearCombo(contactControl.get_cboLookup());
            }
          }
        });
        accntControl.get_cboLookup().raiseEvent("selectedIndexChanged", {
          get_value:function(){return accntControl.get_value()},
          get_item:function(){return accntControl.get_cboLookup().get_selectedItem()}
        });
      }
    });
    

    Let me know if you have any issues.

    Regards,
    Clinton
8 posts, 1 answered