web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Applying lookup filter when option set value equal is 1

(0) ShareShare
ReportReport
Posted on by 365
function onChangeTipologiaintervento(executionContext)
{
    debugger;
    var formContext=executionContext.getFormContext();
    if(formContext.getAttribute("new_tipologiaintervento").getValue!=null){ 
    var tipologiainterventoformContext.getAttribute("new_tipologiaintervento").getValue();
    if (tipologiaintervento == 1 )
    {
        //formContext.getControl("msdyn_workordertype").addPreSearch(filterlookup);
        var fetchxml=  "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>"+
        "<entity name='opportunity'>"+
          "<attribute name='name' />"+
          "<attribute name='customerid' />"+
          "<attribute name='estimatedvalue' />"+
          "<attribute name='statuscode' />"+
          "<attribute name='opportunityid' />"+
          "<order attribute='name' descending='false' />"+
          "<filter type='and'>"+
            "<condition attribute='msdyn_workordertype' operator='in'>"+
              "<value uiname='Intervention' uitype='msdyn_workordertype'>{03A27F86-75C7-EB11-BACC-000D3A26E00B}</value>"+
              "<value uiname='Sopralluogo' uitype='msdyn_workordertype'>{E4997BC9-6DBC-EB11-BACC-000D3A4980A5}</value>"+
            "</condition>"+
          "</filter>"+
        "</entity>"+
      "</fetch>";
     formContext.getControl("msdyn_workordertype").addCustomFilter(fetchxml);
      
    }
}

}
it not working and no error is coming 
Any help would be great!!!!!
I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    you are missing () in this line

      if(formContext.getAttribute("new_tipologiaintervento").getValue!=null){

    so it should be

      if(formContext.getAttribute("new_tipologiaintervento").getValue()!=null){

  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Hi,

    As Guido mentioned you are missing () at  if(formContext.getAttribute("new_tipologiaintervento").getValue!=null). Also, you can  try the below code for filtering:

        var customerAccountFilter = "<filter type='and'><condition attribute='accountcategorycode' operator='eq' value='1'/></filter>";

       formContext.getControl("parentaccountid").addCustomFilter(customerAccountFilter, "account");

    replace the account info with opportunity details

  • unbeatable Profile Picture
    365 on at

    I have tried but it's not filtering the record

    function onChangeTipologiaintervento(executionContext)

    {

       debugger;

       var formContext=executionContext.getFormContext();

       if(formContext.getAttribute("new_tipologiaintervento").getValue()!=null){

       var tipologiaintervento= formContext.getAttribute("new_tipologiaintervento").getValue();

       if (tipologiaintervento == 1 )

       {

           //formContext.getControl("msdyn_workordertype").addPreSearch(filterlookup);

         var fetchxml = "<filter type='and'><condition attribute='msdyn_workordertype' operator='eq' value='03A27F86-75C7-EB11-BACC-000D3A26E00B'/></filter>";

        formContext.getControl("msdyn_workordertype").addCustomFilter(fetchxml,"opportunity");

       }

    }

    }

  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Try to debug the code and provide the console error logs for more details

  • unbeatable Profile Picture
    365 on at

    pastedimage1625473529745v1.png

    See no error is coming

    pastedimage1625473676369v2.png

  • Verified answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Please try this with your record id of work order type. It is should be working fine

    function onChangeTipologiaintervento(executionContext) {
      debugger;
    
      var formContext = executionContext.getFormContext();
      if (formContext.getAttribute("new_tipologiaintervento").getValue() != null) {
        var tipologiaintervento = formContext
          .getAttribute("new_tipologiaintervento")
          .getValue();
    
        if (tipologiaintervento == 1) {
          formContext.getControl("msdyn_workordertype").addPreSearch(function () {
            AddFilter("msdyn_workordertype", formContext);
          });
        }
      }
    }
    
    function AddFilter(fieldName, formContext) {
      if (fieldName != null || fieldName != undefined) {
        fetchQuery =
          "";
        formContext.getControl(fieldName).addCustomFilter(fetchQuery);
      }
    }
    

  • unbeatable Profile Picture
    365 on at
    The scenario is, there is one option set field, and when I select option set value based on that  a lookup value should filter
    function Tipologiaintervento(executionContext)
    {
        debugger;
       var formContext=executionContext.getFormContext();
     if(formContext.getAttribute("new_tipologiaintervento").getValue!=null){ 
        var tipologiainterventoformContext.getAttribute("new_tipologiaintervento").getValue();
        if (tipologiaintervento == 1 )
            {
             formContext.getControl("msdyn_workordertype").addPreSearch(filterlookup());
            }
        else
            {
             formContext.getControl("msdyn_workordertype").removePreSearch(filterlookup());
            }
        } 
    }
    function filterlookup()
    {
        debugger;
       
        var fetchxml = "<filter type='and'>"+
        "<condition attribute='msdyn_workordertype' operator='eq' value='03A27F86-75C7-EB11-BACC-000D3A26E00B'/>"+
        "<condition attribute='msdyn_workordertype' operator='eq' value='E4997BC9-6DBC-EB11-BACC-000D3A4980A5'/>"+
        "</filter>";
     formContext.getControl("msdyn_workordertype").addCustomFilter(fetchxml);
    }
  • Suggested answer
    David Yu Profile Picture
    Microsoft Employee on at

    Hi,

    Can you please check the unique value in the field definition of "new_tipologiaintervento" ?

    You need to use the unique value instead of the display value "1" to work on the filter.

    pastedimage1625538518943v1.png

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    You have plenty of places to fix.

    1. if(formContext.getAttribute("new_tipologiaintervento").getValue!=null){

    I believe you wanted to compare not the method but the the value returned instead. So I believe it should look like:

    if(formContext.getAttribute("new_tipologiaintervento").getValue() != null){

    2. formContext.getControl("msdyn_workordertype").addPreSearch(filterlookup());

    and

    formContext.getControl("msdyn_workordertype").removePreSearch(filterlookup());

    according to the SDK addPreSearch and removePreSearch accept function as a parameter. When you add () after the function name - it is called so the result of the call is passed inside of the function and not the function itself. The proper way to call it either

    formContext.getControl("msdyn_workordertype").addPreSearch(filterlookup);

    or

    formContext.getControl("msdyn_workordertype").addPreSearch(function(){filterlookup();});

    it's up to you to decide which approach to use.

    3. function filterlookup()

    //some additional code here

    formContext.getControl("msdyn_workordertype").addCustomFilter(fetchxml);

    You use formContext in the code but it is not available so your code will always fail. As a possible option you can transform your code to the following:

    formContext.getControl("msdyn_workordertype").addPreSearch(function(){ filterlookup(formContext);});

    //some additional code here

    function filterlookup(formContext){

    //some more code here

  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Hello,

    are you getting any errors with the code that I shared with you? I have tested the same code and it was working fine on my end. The issue might be because of your fetchxml condition. You have to get the fetch from the "Workordertype" entity instead of the opportunity.  This is the correct condition:

    "<filter type='and'><condition attribute='msdyn_workordertypeid' operator='eq' uitype='msdyn_workordertype' value='{E14C2718-B0DD-EB11-BACB-000D3A5601E0}' /></filter>";

    You just have to replace the record Id

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 180 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 123

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans