Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Unable to clear lookup on Change with Javascript

Posted on by Microsoft Employee

Hi all,

I have a lookup field which is filtered by an optionset using this function :

function preFilterLookup() {
    Xrm.Page.getControl("lookupAttribute").addPreSearch(function () {
        addLookupFilter();
    });
}

function addLookupFilter() {

    var optionSet= Xrm.Page.getAttribute("optionSetAttribute").getValue();

    if (optionSet!= null) {

        fetchXml = "<filter type='and'><condition attribute='optionSetAttribute' operator='eq' value='" + optionSet+ "' /></filter>";
        Xrm.Page.getControl("lookupAttribute").addCustomFilter(fetchXml);

I wanted to have it so that if we change the option Set then the look up is emptied so that the user can choose from a new set of values. I did it with :

function optionSet_onChange() {
    var lookUp= Xrm.Page.getAttribute("lookupAttribute");
    if (lookUp!= null) {
        lookUp.setValue(null);
    }

However the field is never reset to null. The weirdest part is that it did work a few times at first but since then nothing.

Do any of you have any idea how that could of happened ?

Cheers

Simon

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to clear lookup on Change with Javascript

    Thanks !

    That was the problem.

    Once I definied the parameter in the CRM and not in the on_change function, it worked

    Thanks again !

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to clear lookup on Change with Javascript

    pls find the link pass parameter to clear lookup

    crmdm.blogspot.in/.../as-you-all-know-one-of-best-features-of.html

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to clear lookup on Change with Javascript

    try to debug the code and find were exactly you get error, sent the screen shot of error

    function preFilterLookup()

    {

       Xrm.Page.getControl("lookupAttribute").addPreSearch(function () {

           addLookupFilter();

       });

    }

    function addLookupFilter() {

       var optionSet= Xrm.Page.getAttribute("optionSetAttribute").getValue();

       if (optionSet!= null) {

           fetchXml = "<filter type='and'><condition attribute='optionSetAttribute' operator='eq' value='" + optionSet+ "' /></filter>";

           Xrm.Page.getControl("lookupAttribute").addCustomFilter(fetchXml);

    }

    }

    //I wanted to have it so that if we change the option Set then the look up is emptied so that the user can choose from a new set of values. I did it with :

    function optionSet_onChange()

    {

       var lookUp= Xrm.Page.getAttribute("lookupAttribute");

       if (lookUp!= null) {

           Xrm.Page.getAttribute("lookupAttribute").setValue(null);

       }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to clear lookup on Change with Javascript

    Hi,

    Thanks for your answer.

    I tried what you said but same result : nothing happened.

    I also tried putting alert() at different places but nothing showed...

    I'm new to dynamics so I'm not sure if the alert() function doesn't show in CRM or if my code is just not even being executed

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Unable to clear lookup on Change with Javascript

    try to set null value to lookup in alternate method

    function SetLookupNull(lookupAttribute)

    {var lookupObject = Xrm.Page.getAttribute(lookupAttribute);

    if (lookupObject != null)

    {

    Xrm.Page.getAttribute(lookupAttribute).setValue(

    null);

    }

    }

    regards,

    Nithin

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