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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Javascript for filtering lookup using option set

(0) ShareShare
ReportReport
Posted on by 8

I have the following code below, unfortunately i can't get it to work.

On my form I have an option set called:

Schema nameCaseTypeCode
Logical name: casetypecode
and
Schema name: xpg_CaseSubtype
Logical name: xpg_casesubtype
xpg_casesubtype is a lookup field and those records have a field called casetypecode as well using the same option set.
I want to filter xpg_casesubtype records by which casetypecode is chosen
I have come up with the following JS, but it doesnt seem to work, theres no error.



function filterLookupByOptionSet(executionContext) {
    var formContext = executionContext.getFormContext();
    var caseTypeCode = formContext.getAttribute("casetypecode").getValue();
	
    if (caseTypeCode != null) {
        var filter = "" "" "";
        formContext.getControl("xpg_casesubtypeid").addCustomFilter(filter);
    }
	else {
		formContext.getAttribute("xpg_casesubtype").setValue(null);
	}
}

I have the same question (0)
  • Verified answer
    Eiken Profile Picture
    on at

    Hi,

    The value obtained by "var caseTypeCode = formContext.getAttribute("casetypecode").getValue();"  is an object whose first element is an array containing information about the lookup field.

    This array contains the id(current record id), name(current value of lookup field) and entitytype(the logical name of the entity)

    So I edit your code.

    var filter = "<filter type='and'>"+"<condition attribute='xpg_casetypecode' operator='eq' value='" + caseTypeCode[0].id + "' />"+"</filter>";


    or

    var filter = "<filter type='and'>"+"<condition attribute='xpg_casetypecode' operator='eq' value='" + caseTypeCode[0].name + "' />"+"</filter>";

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at

    Hello,

    addCustomFilter() must be called in a addPreSearch handler.

    If you call addCustomFilter() in an on load event (for example), it won't work.

    See learn.microsoft.com/.../addcustomfilter

  • Suggested answer
    Eiken Profile Picture
    on at

    Hi,

    Did my answer help you ?

    If yes, please don't forget to click Yes under "Did this answer your question?" to close this thread.

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 49 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
#ManoVerse Profile Picture

#ManoVerse 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans