web
You’re offline. This is a read only version of the page.
close
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.

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans