Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear Lookup field in MS Dynamics CRM 4.0 using JavaScript

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 = [];
like image 454
Greg McGuffey Avatar asked Feb 26 '23 23:02

Greg McGuffey


1 Answers

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 = [];
like image 55
EndangeredMassa Avatar answered Mar 09 '23 00:03

EndangeredMassa