views:

781

answers:

3

I'm trying clear the value of a lookup field via Javascript. I've tried this:

crmForm.all.new_mylookupfield.DataValue = null;

But that isn't working. I inspected the DataValue of the lookup when it was in fact cleared and it returned a null.

alert(document.getElementById("new_mylookupfield").DataValue == null); // true

I'm must be missing something here....

Thanks for the help!

UPDATE: I finally got around to testing some of the suggestions. I'm not sure what I was doing wrong initially, but both of these methods work to clear a lookup via JavaScript:

crmForm.all.new_mylookupfield.DataValue = null;
crmForm.all.new_mylookupfield.DataValue = [];
+3  A: 

I don't remember having to do this, but have you tried setting the value to just a new Array() with length zero?

Matt
A: 

Did you ever receive an answer to clearing a lookup field?

Nancy
I've had some other projects come up and haven't gotten to test any of the suggestions yet. I might get to that later today.
Greg McGuffey
See the my original post for an update and also the accepted answer for two different ways to handle this.
Greg McGuffey
+4  A: 

Lookup controls have a specific type of object for their DataValue. It's an array of objects that look like this:

{
    id: /* item id */,
    typename: /* entity type name */,
    name: /* text to display in link */
}

If you want to remove all values from the lookup, you can set it to null, but it's better to just set it to an empty array.

If you assign the value, but it doesn't seem to change anything, then you are probably not typing the correct id for the attribute. For example: If I have an entity with a lookup attribute of sneakers_brokerid, then I need to assign that value like so:

 crmForm.all.sneakers_brokerid.DataValue = [];
EndangeredMassa