locked
Filter CRM 2011 Lookup using OptionSetValue selected text RRS feed

  • Question

  • I need to filter a lookup field's values based on the selected text from one or two optionsets/dropdown list in CRM 2011. Has anyone got experience of doing this? Everything i've found on the web is related to filtering using another lookup field.
    Tuesday, July 5, 2011 3:52 PM

Answers

All replies

  • Hi,

    Refer to the following blog post for solution: http://crmstuff.blogspot.com/2011/01/create-custom-filtered-lookup-view-in.html


    Jehanzeb Javeed

    http://worldofdynamics.blogspot.com
    Linked-In Profile |CodePlex Profile

    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Tuesday, July 5, 2011 4:12 PM
  • Hi

    Any chance you understand the code enough to advice me on where I'm going wrong? I have edited the code from the link you posted and adapted it to use the value of the selected optionSetValue for the FetchXml filter. but I keep getting errors. Thanks in advance. Code below...

    var FilterBy = MakeStruct("SchemaName Operator Value");
    var ViewColumn = MakeStruct("SchemaName Width");

    // Advanced Filtered Lookup
    function AdvancedFilteredLookup(lookupSchemaName, viewId, entityName, primaryKeyName, primaryFieldName, viewDisplayName, filterBy, orderBy, viewColumns) {
        var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
    "<entity name='" + entityName + "'>" +
    "<attribute name='" + primaryFieldName + "' />" +
    "<order attribute='" + orderBy + "' descending='false' />" +
    "<filter type='and'>" +
    "<filter type='and'>";
        for (var i = 0; i < filterBy.length; i++)
            fetchXml += "<condition attribute='" + filterBy[i].SchemaName + "' operator='" + filterBy[i].Operator + "' value='" + filterBy[i].Value + "' />";
        fetchXml += "</filter></filter></entity></fetch>";

        var layoutXml = "<grid name='resultset' " +
    "object='1' " +
    "jump='name' " +
    "select='1' " +
    "icon='1' " +
    "preview='1'>" +
    "<row name='result' " +
    "id='" + primaryKeyName + "'>";
        for (var i = 0; i < viewColumns.length; i++)
            layoutXml += "<cell name='" + viewColumns[i].SchemaName + "' width='" + viewColumns[i].Width.toString() + "' />";
        layoutXml += "</row></grid>";

        try {
            var lookupControl = Xrm.Page.ui.controls.get(lookupSchemaName);
            lookupControl.addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
        }
        catch (err) {
        }
    }


    ////////////////////////////////////////////////////////////////////


    function FilterProducts() {
        try {
            // Parameters
            var customViewId = "{86783E3C-73AC-E011-9F4C-000C29BB097B}";
            var customViewName = "Filtered Products";
            var lookupFieldName = "new_product";
            var entityName = "new_pcholiday";
            var primaryKeyName = "new_pcHolidayid";
            var primaryFieldName = "new_name";
            var orderBy = "new_name";

            // Generate Filters
            var status = Xrm.Page.getAttribute("new_status").getSelectedOption().value;
            var region = Xrm.Page.getAttribute("new_region").getSelectedOption().value; 
            var cruiseline = Xrm.Page.getAttribute("new_cruiseline").getSelectedOption().value;
            var filters = new Array();

            var index = 0;
            if (status != null && status != "")
                filters[index++] = new FilterBy("new_status", LogicalOperator.Eq, status);
            if (region != null && region != "")
                filters[index++] = new FilterBy("new_region", LogicalOperator.Eq, region);
            if (cruiseline != null && cruiseline != "")
                filters[index++] = new FilterBy("new_cruiseline", LogicalOperator.Eq, cruiseline);
           
            // View Columns
            var viewColumns = [new ViewColumn("new_region", 150), new ViewColumn("new_cruiseline", 150), new ViewColumn("new_name", 200), new ViewColumn("new_departuredate", 100), new ViewColumn("new_noofnights", 100), new ViewColumn("new_type", 75)];

            // Create Dynamics View
            AdvancedFilteredLookup(lookupFieldName, customViewId, entityName, primaryKeyName, primaryFieldName, customViewName, filters, orderBy, viewColumns);

            // Clear Previous Product Value
            crmForm.all.item(lookupFieldName).DataValue = null;
        }
        catch (err) {
        }
    }

     

    Tuesday, July 12, 2011 2:11 PM