Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Filtered lookups (again)

(0) ShareShare
ReportReport
Posted on by 20

Hi

I have tried adapting bit of JS that I was helped with previously to filter down the results on a lookup field, but it's not returning the results I was expecting, in fact it doesn't appear to be doing anything, but it's not throwing up an errors either, it did when I first put it on there as there was a small problem which I corrected so I know the script is loading.

I have a table tms_courses (PK: tms_courseid) these courses are linked to accounts and opportunities through a join table tms_joincoursebookings (PK: tms_joincoursebookingsid, Account: tms_account, Course: tms_course, Opportunity: tms_opportunity)

Against the course I also assign delegates, these are joined through a table tms_coursedelegates, against the entry in this table I also want to record the account and opportunity that the delegate relates to. tms_coursedelegates (PK: tms_coursedelegatesid, Account: tms_account, Course: tms_course, Opportunity: tms_opportunity).

The delegate is assigned to the course through a quick create form which is viewed from a subgrid view through the main form for the course.

The open record for the course pulls through to the form, what I want to do is be able to filter down the opportunity field based upon the course that is selected. So i've adapted the code below to try and filter the opportunity lookup but it's doing anything, hoping this is something simple I've done wrong as this is new to me still.

Thinking about it now, I suppose what i really need to do, is filter the account field based upon the course as above, and then filter the opportunity based upon the course and account after the account has been entered, hope that makes sense.

var filter = "";
var formContext;
function filterCourseOpportunities(executionContext) {
    formContext = executionContext.getFormContext();
    var course = formContext.getAttribute("tms_course").getValue(); /* references the calling form control, in this case the subject field */
    if (course != null) {
        var fetchXML = ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            ""  
            "";
        fetchXML = "?fetchXml="   encodeURIComponent(fetchXML);
        Xrm.WebApi.retrieveMultipleRecords("opportunity", fetchXML).then(
            function success(data) {
                filter = "";
                for (var i = 0; i < data.entities.length; i  ) {
                    filter  = "";
                }
                filter  = "";
            },
            function error(e) {
                alert("Error in retrieving opportunities.");
            }
        );
        formContext.getControl("tms_opportunity").addPreSearch(filterOpportunities); /* references the control to have the filter applied to */
    }
    else {
        filter = "";
        formContext.getAttribute("tms_opportunity").setValue(null); /* references the control to have the filter applied to */
    }
}

function filterOpportunities() {
    formContext.getControl("tms_opportunity").addCustomFilter(filter, "opportunity"); /* references the control to have the filter applied to */
}

Would appreciate any help people can provide so I can understand it and adapt to future uses as well.

  • Suggested answer
    a33ik Profile Picture
    84,329 Most Valuable Professional on at
    RE: Filtered lookups (again)

    Hello,

    I would recommend to split your code into 2 parts:

    1. Code that handles onload/onchange of tms_course field event handlers that will build the filter variable.

    2. Use that filter variable in filterOpportunities method.

    Also, I won't recommend to rely on formContext variable and pass it directly. As a reference, you can check this post - community.dynamics.com/.../applying-lookup-filter-when-option-set-value-equal-is-1

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,245 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,925 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans