Set a lookup to NULL

Quite often I see this question in the newsgroup: How do I set a lookup to a NULL value?

Here is the answer for when you're working in server side code:


Entity.Lookup = new Lookup();
Entity.Lookup.IsNull = true;
Entity.Lookup.IsNullSpecified = true;


this also works for customers:


contact.parentcustomerid = new Customer();
contact.parentcustomerid.IsNull = true;
contact.parentcustomerid.IsNullSpecified = true;


If you're working in a client side javascript, then use this approach:


crmForm.all.lookupschemaname.DataValue = null;


Good luck!

8 comments:

Maria said...

Thank you! This was exactly what I was looking for :)

ronauger said...

Yes but how do you set a lookup field to a guid in javascript?

srini said...

Hi,
I've added below code, but never work, pls let me know anything I miss

opportunity.originatingleadid = new Lookup();
opportunity.originatingleadid.IsNull = true;
opportunity.originatingleadid.IsNullSpecified = true;

Thanks

Ronald Lemmen said...

Hi Srini,

The originatingleadid attribute is not valid for updating. This is documented in the sdk. Is there a specific reason why you would not store the origin of the opportunity? You could remove the attribute from the screen and most of your users will not be able to see the origin of the opportunity. Only when users change the advanced find view or use Excel to connect to CRM for instance they can get this info back.

Kind regards,
Ronald Lemmen

Anonymous said...

You forgot to set two attributes:

Entity.Lookup.DoYouReallyWantToSetItToNull = true;

Entity.Lookup.DidYouReallyWantToSetDoYouReallyWantToSetItToNull = true;


I mean really, how many times do you have to tell the damn thing to set to null? :(

Anonymous said...

hey,

i have tried to set the value in javascript onload and with help of an update request.

(opentag)new_currentuserid IsNull="true"(closetag)00000000-0000-0000-0000-000000000000(opentag)/new_currentuserid(closetag)


thats exactly the code, which i receive if i use the javascript-webservice builder from stunnware and your request properties:

inc.new_currentuserid = new Lookup();
inc.new_currentuserid.IsNull = true;
inc.new_currentuserid.IsNullSpecified = true;

but if i use this request in my javascript-code it does not work.

i already have debugged into the js and the request is exactly the same.

kind regards,
mk

Anonymous said...

i manually added the "IsNullSpecified". seems to be working! ;)

chaitanya said...

Hi Ronald,

I'm trying to update a lookup field of an Entity to some value which is empty initially. I'm using QueryByAttribute expression for filtering the records placing record attributes in Columnset. However since the lookup field was empty initially am not able to get the schema names while am assigning them a value. Kindly advice.

Thanks,
Chaitanya