Skip to main content

Notifications

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);
	}
}

  • Suggested answer
    Eiken Profile Picture
    on at
    RE: Javascript for filtering lookup using option set

    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.

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at
    RE: Javascript for filtering lookup using option set

    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

  • Verified answer
    Eiken Profile Picture
    on at
    RE: Javascript for filtering lookup using option set

    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>";

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Product updates

Dynamics 365 release plans