Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

addcustomview in addpresearch removes value from the lookup after saving the code.

Posted on by 81

Hello guys,

Today I faced some weird behavior of a javascript function provided by Microsoft maybe I am making it wrong. I am using addPreSearch for a lookup on my custom entity to add a custom view using addCustomView function and that's working fine for filtering of records but when I select the record and save that The lookup resets its selected value and gets empty. I don't why this happening. The code for this is below:

function preFilterLookup(econtext) {
    debugger;
    // add the event handler for PreSearch Event
    Xrm.Page.getControl("nfs_product").addPreSearch(addFilter);
}

function addFilter() {
    //check if the city is not empty
    debugger;

    var functionName = "setCustomView";
    var viewId = "{00000000-0000-0000-0000-000000000001}";

    var entityName = "product";

    var viewDisplayName = "Products from Price list";

    var fetchXml = "<fetch mapping='logical' version='1.0' distinct='true' output-format='xml-platform'> " +

        "<entity name='product'>" +
        "<attribute name='name' />" +
        "<attribute name='productid' />" +
        "<attribute name='productnumber' />" +
        "<attribute name='description' />" +
        "<attribute name='statecode' />" +
        "<attribute name='productstructure' />" +
        "<order descending='false' attribute='productnumber' />" +
        "<link-entity name='productpricelevel' to='productid' from='productid' alias='ae' link-type='inner'>" +
        "<filter type='and'>" +
        "<condition attribute='pricelevelid' operator='eq' uitype='pricelevel' value='" + Xrm.Page.data.entity.attributes.get("nfs_pricelist").getValue()[0].id + "' />" +
        "</filter>" +
        "</link-entity>" +
        "</entity >" +
        "</fetch >";


    var layoutXml = "<grid name=' resultset' object='1' jump ='productnumber' select ='1' icon ='1' preview ='1'>" +
        "<row name='result' id=' productid'>" +
        "<cell name='name' width='150' />" +
        "<cell name='description' width='150' />" +
        "<cell name='statecode' width='150' />" +
        "</row>" +
        "</grid >";


    Xrm.Page.getControl("nfs_product").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);


}




*This post is locked for comments

  • Roxana Profile Picture
    Roxana 689 on at
    RE: addcustomview in addpresearch removes value from the lookup after saving the code.

    Hi, if you want to return to the original view. I recommend not to call the "method where the fetchXML" is.

    When I don´t call the method, the default preview, set in "Properties" of the field, will show up.

    Thank you.

  • Shyam D Profile Picture
    Shyam D 15 on at
    RE: addcustomview in addpresearch removes value from the lookup after saving the code.

    Hi,

    Thanks. I found it helpful. But in addition to this, I want to reset the lookup filter to the original state....that is I need to remove the applied filter. What lines of code will be needed for this?

  • Roxana Profile Picture
    Roxana 689 on at
    RE: addcustomview in addpresearch removes value from the lookup after saving the code.

    Thank you it was very helful. 

  • Verified answer
    Naveen Goyal Profile Picture
    Naveen Goyal 81 on at
    RE: addcustomview in addpresearch removes value from the lookup after saving the code.

    After investigation I have finally found two problems inside the customer script:

    -              Inside the layout definition I have added an undesired space:  id=’ productid’

    -              The productid should not be displayed into the grid.

    Existing Code :

    function addFilter()
    {
       debugger;
       var functionName = "setCustomView";
       var viewId = "{00000000-0000-0000-0000-000000000001}";
       var entityName = "product";
       var viewDisplayName = "Products from Price list";
       var fetchXml = "<fetch mapping='logical' version='1.0' distinct='true' output-format='xml-platform'> " +
           "<entity name='product'>" +
           "<attribute name='name' />" +
           "<attribute name='productid' />" +
           "<attribute name='productnumber' />" +
           "<attribute name='description' />" +
           "<attribute name='statecode' />" +
           "<attribute name='productstructure' />" +
           "<order descending='false' attribute='productnumber' />" +
           "<link-entity name='productpricelevel' to='productid' from='productid' alias='ae' link-type='inner'>" +
           "<filter type='and'>" +
           "<condition attribute='pricelevelid' operator='eq' uitype='pricelevel' value='" + Xrm.Page.data.entity.attributes.get("nfs_pricelist").getValue()[0].id + "' />" +
           "</filter>" +
           "</link-entity>" +
           "</entity >" +
           "</fetch >";
       var layoutXml = "<grid name=' resultset' object='1' jump ='productnumber' select ='1' icon ='1' preview ='1'>" +
           "<row name='result' id=' productid'>" +
           "<cell name='name' width='150' />" +
           "<cell name='description' width='150' />" +
           "<cell name='productnumber' width='120' />" +
           "<cell name='productid' width='120' />" +
           "<cell name='statecode' width='150' />" +
           "</row>" +
           "</grid >";
       Xrm.Page.getControl("nfs_product").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
    }

    To fix this issue replace the above definition by the below one  :

    function addFilter()
    {
       debugger;
       var functionName = "setCustomView";
       var viewId = "{00000000-0000-0000-0000-000000000001}";
       var entityName = "product";
       var viewDisplayName = "Products from Price list";
       var fetchXml = "<fetch mapping='logical' version='1.0' distinct='true' output-format='xml-platform'> " +
           "<entity name='product'>" +
           "<attribute name='name' />" +
           "<attribute name='productid' />" +
           "<attribute name='productnumber' />" +
           "<attribute name='description' />" +
           "<attribute name='statecode' />" +
           "<attribute name='productstructure' />" +
           "<order descending='false' attribute='productnumber' />" +
           "<link-entity name='productpricelevel' to='productid' from='productid' alias='ae' link-type='inner'>" +
           "<filter type='and'>" +
           "<condition attribute='pricelevelid' operator='eq' uitype='pricelevel' value='" + Xrm.Page.data.entity.attributes.get("nfs_pricelist").getValue()[0].id + "' />" +
           "</filter>" +
           "</link-entity>" +
           "</entity >" +
           "</fetch >";
       var layoutXml = "<grid name=' resultset' object='1' jump ='productnumber' select ='1' icon ='1' preview ='1'>" +
           "<row name='result' id='productid'>" +
           "<cell name='name' width='150' />" +
           "<cell name='description' width='150' />" +
           "<cell name='productnumber' width='120' />" +
           "<cell name='statecode' width='150' />" +
           "</row>" +
           "</grid >";
       Xrm.Page.getControl("nfs_product").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
    }

    IMPORTANT

    You should be informed that Xrm.Page is now deprecated, so instead customer should use ExecutionContext.getFormContext

    docs.microsoft.com/.../important-changes-coming

    Deprecated Client API Replacement Client API Comments
    Xrm.Page

    Forms: ExecutionContext.getFormContext

    Commands: Send it as the PrimaryControl parameter

    Xrm.Page is the primary form context. If a script is run on a secondary context (grid row, quick form, related entity) then Xrm.Page will be for the wrong form context. By using alternate methods of getting the form context we allow the same script to be used without modification in all contexts
  • Naveen Goyal Profile Picture
    Naveen Goyal 81 on at
    RE: addcustomview in addpresearch removes value from the lookup after saving the code.

    If anyone can send me working code for addCustomView with addPreSearch, that is already tested. That will be a very much helpfull for me.

  • Naveen Goyal Profile Picture
    Naveen Goyal 81 on at
    RE: addcustomview in addpresearch removes value from the lookup after saving the code.

    No that's the only script on that form, as It's a custom entity so this form doesn't have any prebuilt javascript files with it.

  • Shaminderpal Singh Profile Picture
    Shaminderpal Singh 1,565 on at
    RE: addcustomview in addpresearch removes value from the lookup after saving the code.

    Hi Naveen,

    Are you having any other js on save or on load ?There might be some line of code that clears the value.Check both on load as well as on save.

    Regards,

    Shaminder

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans