How do I pass lookup object values into a FetchXml query?

1.5k views Asked by At

I am trying to filter a lookup field products with another lookup field editorial (my company sells textbooks).

I know how to retrieve values from the lookup object but I do not know how to pass them into my fetchXml query statement.

function preFilterLookup()
{
    Xrm.Page.getControl("new_editorialsearch").addPreSearch(function () { 

        var ediObject = Xrm.Page.getAttribute("new_editorialsearch").getValue();

        // Get the lookup Object, and extract the Value (ID and Text)
        if(ediObject != null)
        {
            var ediTextValue = ediObject[0].name;
            var ediID = ediObject[0].id;

            // Filter products by editorial
            fetchXml = "<filter type='and'><condition attribute='name' operator='eq' value='" + ediTextValue + "' /></filter>";

            // Apply the filter to the field
            Xrm.Page.getControl("new_engpro").addCustomFilter(fetchXml);
        }        
    });
}

I know I'm doing something wrong because CRM keeps telling me my function is undefined, and I can't see any syntax errors.

Can somebody please tell me the proper way to pass the object name and id into the fetchXml statement?

1

There are 1 answers

0
Polshgiant On

addCustomFilter can only be called from the addPreSearch event of the same control. So instead of calling addPreSearch on new_editorialsearch, call it on new_engpro.