javascriptdynamics-crmmicrosoft-dynamicsdynamics-crm-webapidynamics-crm-365-v9

addPreSearch filter not applying


I am trying to use the addPreSearch function to add a custom filter to a lookup field, but the function does not seem to execute fully before the results of the lookup are displayed. The code for this looks something like this:

function onFieldChange(executionContext) {
    var formContext = executionContext.getFormContext();
    formContext.getControl("test_code").removePreSearch(testFunctionFilter);
    formContext.getControl("test_code").addPreSearch(testFunctionFilter);
}


function testFunctionFilter(executionContext) {
    var formContext = executionContext.getFormContext();

    var record1 = formContext.getAttribute("test_record1_link").getValue(); //get linked record
    var record1FullId, record1Id, stringRecordId, idLength, record1Guid = "0";
    if (record1 != null)  {
        record1Id = record1[0].id;
        record1Id = record1FullId.slice(1, -1);
        stringRecordId = record1FullId.toString();
        idLength = stringRecordId.length;


        //Guid when retrieved from tablet does not have parenthesis on each end
        if (idLength == 36) {
            record1Guid = record1FullId;
        } else {
            record1Guid = recordId;
        }
    }

    var fieldValue;
    Xrm.WebApi.retrieveRecord("test_record1", record1Guid, "?$select=test_field1")
        .then(function(result1) {
            fieldValue = result1.test_field;
            var options = generateOptions(executionContext, fieldValue); //creates option string using retrieved fieldValue
            Xrm.WebApi.retrieveMultipleRecords("test_record2", options)
                .then(function(result) {
                    var codes = getCodes(result2, fieldValue);
                    filter = generateFilter(codes, record1Guid); //creates custom filter using provided parameters
                    console.log(filter); //displays filter correctly
                    formContext.getControl("test_codelookup").addCustomFilter(filter, "test_coderecord"); //not working?
            });
        });
}

The filter is generated correctly using the functions used above whose definitions aren't shown. That isn't the issue. I've tried creating a separate test function where I hard coded one of the filters that the function above generated, and the lookup displayed the correct results. The testFunctionFilter should run to completion before the results of the lookup are displayed, correct? Because the filter is logged to the console after the results of the lookup appear. Are the nested asynchronous Xrm.WebApi calls somehow causing the issue? I'm not quite sure what is wrong. Please advise.


Solution

  • You are right. Xrm.WebApi calls are always Asynchronous, which is unusable in this case of adding dynamic filter using addCustomFilter.

    You have to use XMLHttpRequest and make that call as Synchronous by setting third parameter as false like below:

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Utility.getGlobalContext().getClientUrl() + 
                              "/api/data/v9.0/test_record1?$select=test_field1", false);