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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Unable to clear a lookup value using js

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Suggested answer
    ashlega Profile Picture
    34,477 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

  • sahara Profile Picture
    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.

  • gdas Profile Picture
    50,091 Moderator 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
    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?

  • Verified answer
    Temmy Wahyu Raharjo Profile Picture
    2,916 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
    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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans