locked
Related Record Filtering not working after setting lookup with Javascript RRS feed

  • Question

  • I have 2 lookups on a form (Quick Create) and I'm setting the primary lookup via javascript, the child record with related record filtering is not applying the filter and instead is showing every record.  If I click on the parent lookup and just click ok (no change to value) then the child lookup works.  I suspect the child is not catching the fact the parent changed.  Any suggestions on how to force it to see the change?  An example of this would be State and City as parent/filtered child lookup. 

    This is 2016 On Prem

    function setScopeOfProject() {

        var lookup = new Array();

        var recordid = "6FC58366-EA2D-E611-80CD-005056862159";

        var recordname = "Scope of Project";

        var entityname = "ocsd_prioritizationtype";

        lookup[0] = new Object();

        lookup[0].id = recordid;

        lookup[0].name = recordname;

        lookup[0].entityType = entityname;

        Xrm.Page.getAttribute("ocsd_prioritization").setValue(lookup);

    }


    David J

    Wednesday, June 29, 2016 4:50 PM

All replies

  • I am also facing exactly same issue.
    Thursday, July 7, 2016 6:52 PM
  •  Hi David,

    You can probably try below solution. Call function contactLookupFilter() onLoad of the form:-

    function contactLookupFilter() {
        (function preFilterLookup() {
            Xrm.Page.getControl("parentcustomerid").addPreSearch(addLookupFilter);
        })();
    
        function addLookupFilter() {
            var contactFilter = "<filter type='and'><condition attribute='contactid' operator='" + parentFieldLookupValue + "' /></filter>";
            Xrm.Page.getControl("parentcustomerid").addCustomFilter(contactFilter, "contact");
        }
    }

    -Krutika


    • Edited by Krutika S Friday, July 8, 2016 4:13 AM
    • Proposed as answer by Krutika S Thursday, July 21, 2016 5:04 AM
    Friday, July 8, 2016 4:12 AM
  • You would need to write the similar logic in the script based on your requirement as follows.
     
    function filterCustomerAccounts() {
        //Only show accounts with the type 'Preferred Customer'
        var customerAccountFilter = "<filter type='and'><condition attribute='accountcategorycode' operator='eq' value='1'/></filter>";
        Xrm.Page.getControl("parentaccountid").addCustomFilter(customerAccountFilter, "account");
    }
    //set setParentAccountIdFilter in the Opportunity form Onload event handler
    function setParentAccountIdFilter() {
        Xrm.Page.getControl("parentaccountid").addPreSearch(filterCustomerAccounts);
    }
     
    Call the function “setParentAccountIdFilter” on OnLoad event of the entity form. So this way it would filter the child lookup based on the Parent Lookup value selected.
     

    Hope this helps!

    Thanks,

    Sam


    Inogic | Blog | news@inogic.com

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Friday, July 8, 2016 12:38 PM