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.