Search code examples
dynamics-crmdynamics-crm-onlinedynamics-365

OptionSetValueCollection not taking a null value. Throws Generic SQL error when setting up the value to null


I'm creating a new record in CRM plugin(by reading the data from a related record) and the data that I'm passing may / may not contain "OptionSetValueCollection". Whenever the value for the OptionSetValueCollection is null the IOrganization.Create is throwing a Generic SQL exception.

Currently I'm checking the submitted value for null and when not null I'm not submitting a value for the created object.

My question is why does OptionSetValueCollection not taking null? Is this a platform issue?

I've also tried creating a List<OptionSetValue> object and adding the incoming OptionSetValues from the OptionSetValueCollection and then passing it to the target attribute, tried passing in null and also used the null-coalescing operator all with no luck.

//Earlybound code
Account account = new Account(){
       Name = newBrand,
       new_accounttype = new OptionSetValue((int)new_AccountType.Brand),
       TerritoryId = siteRequestRecord.new_territoryid,
       new_category1 = siteRequestRecord.new_category1 ?? null,
   };
if (category2 != null)
{
     account.new_category2 = siteRequestRecord.new_category2;
}
service.Create(account);

Solution

  • Seems to be a long outstanding issue.

    There is a bug related to multiselect optionset - if you set it to null during creation that will trigger an error. But the same code that sets field to null works fine during update. So if you set it to null during Create just don't set field value and as a result you'll get blank value of a field.