Tuesday, November 24, 2009

Clearing fields in Microsoft Dynamics CRM 4.0

I've used following code to clear fields (set properties of entities to null):

account acc = new account();
acc.accountid = new Key(new Guid("249FB2CF-31CD-DE11-AB59-005056B30605"));

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

acc.primarycontactid = lookup;
crmservice.Update(acc);


This code works but there is more elegant and readable way to clear fields:

account acc = new account();
acc.accountid = new Key(new Guid("249FB2CF-31CD-DE11-AB59-005056B30605"));
acc.primarycontactid = Lookup.Null;
crmservice.Update(acc);


Similar approach also will work for CrmDateTime, CrmNumber, Picklist, Customer, CrmMoney, CrmFloat, CrmDecimal fields.