Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Unable to clear a lookup value using js

Posted on by 645

I am trying to clear a lookup field using JS, when I run my code I get an alert -> an error happened please retry. when I tested my code in Developer mode after this line :

  Xrm.Page.getAttribute("new_activitycategory1id").setValue(null); The function starts again (for no reason) and the field is not cleared (with out this line I get no errors).

my function triggered from onChange event in my JS code, the function works fine and the IF is right, but the setValue line is problematic... I don't understand why.

my code:

function preventSavinf3940category(){    

if(Xrm.Page.getAttribute("new_activitycategory1id") == null){ // this if is alwaus false even after setValue to null

return;
}

		 var category1 =  Xrm.Page.getAttribute("new_activitycategory1id").getValue();
		 var category1Data =  category1[0].id;		    		         
 		 var students = getId(1);
 		 var teachers = getId(2);		         
      	 if(category1Data == teachers || category1Data== students)
           	 {
           	  Xrm.Page.getAttribute("new_activitycategory1id").setValue(null);
	   	  	   alert("this category is not in use anymore")
           	 }
	    					      
    }

*This post is locked for comments

  • sahara Profile Picture
    sahara 645 on at
    RE: Unable to clear a lookup value using js

    you are right, I did the check only on the obj but not the value after adding a value check it works fine

    Thanks

  • Verified answer
    Temmy Wahyu Raharjo Profile Picture
    Temmy Wahyu Raharjo 2,914 on at
    RE: Unable to clear a lookup value using js

    Hi, I think you miss understood about this code: Xrm.Page.getAttribute("new_activitycategory1id") == null

    If you want to chech the value, then you can use

    //If null then return

    if(!Xrm.Page.getAttribute("new_activitycategory1id").getValue()) return;

    When you put this code only (Xrm.Page.getAttribute("new_activitycategory1id")), it will return the attribute. Not the value.

  • sahara Profile Picture
    sahara 645 on at
    RE: Unable to clear a lookup value using js

    @Goutam it doesn't work, it's like a loop every time I try to clear the field I start the function again.. I think because it's connected to onchange event so it starts it all over again, how can I solve that?

  • gdas Profile Picture
    gdas 50,085 on at
    RE: Unable to clear a lookup value using js

    Hi ,

    Your code is correct I just check with alert box as well .  This is really strange behavior.

    May be there are some other customization or Javascript function which  causes the issue .

    try to fired  same function  in another lookup and remove the alert part.

  • sahara Profile Picture
    sahara 645 on at
    RE: Unable to clear a lookup value using js

    Hey Alex, the onchange event fires only if that field (new_activitycategory1id) is not null.

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Unable to clear a lookup value using js

    Hi,

      try checking if it's already null (right after the first getValue) and return from the function if it is.

      Might be a bug there:

    http://sharathdozen.blogspot.ca/2017/04/onchange-javascript-fires-repeatedly.html

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans