Search code examples
javascriptdynamics-crmlookupdynamics-crm-2016xrm

Unable to clear a lookup value using js crm 2016


I'm using Dynamics crm 2016, after onChange event I call a function that check some terms and according to these terms clear a lookup field. the problem is that the lookup isn't getting clear... where is the problem in my code?

function preventSavinf3940category(){    
if(Xrm.Page.getAttribute("new_activitycategory1id") == null){ // this if is 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);- doesn't clear the field
               alert("this category is not in use anymore")
             }

    }

Solution

  • Change this below line

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

    like this

    if(Xrm.Page.getAttribute("new_activitycategory1id") != null && Xrm.Page.getAttribute("new_activitycategory1id").getValue() == null){