Search code examples
c#dynamics-crm-2011dynamics-crm

Update attribute with NULL


I am trying to update a Entity that is an OptionSetValue, the values it will update from are in a radioboxlist and one of the option is blank/null. Is there a way to pass the null value to CRM Entity to update?

This is the radiobox list:

<asp:RadioButtonList RepeatDirection="Vertical" Width="250px" CellPadding="0" CellSpacing="0" ID="rblChangeButtonType" runat="server" AutoPostBack="False" SelectedValue='<%# Bind("EvalVal") %>'>
  <asp:ListItem Value="" Text="No Evaluation Yet" />
  <asp:ListItem Value="100000000" Text="A - Pending order or visit" />
  <asp:ListItem Value="100000001" Text="B - 75% probability of a sale in 6 months" />
  <asp:ListItem Value="100000002" Text="C - 50% probability of a sale in 12 months" />
  <asp:ListItem Value="100000004" Text="F - 0% probability of a sale" />
</asp:RadioButtonList>

And this is the code I am using to update the Entity, it works for all the values but the blank one.

int colderEval = int.Parse(rblChangeButtonType.SelectedValue.ToString());

OptionSetValue selectedEval = new OptionSetValue(colderEval);

Entity opportunity = new Entity("opportunity") { Id = oppGuid };
opportunity["new_colderevaluation"] = selectedEval;

Any idea how to pass the blank one also?


Solution

  • Just check for your special blank value and then set the field to null if necessary:

    Entity opportunity = new Entity("opportunity") { Id = oppGuid };
    
    if (rblChangeButtonType.SelectedValue.ToString() != string.Empty)
    {
      int colderEval = int.Parse(rblChangeButtonType.SelectedValue.ToString());
      OptionSetValue selectedEval = new OptionSetValue(colderEval);
    
      opportunity["new_colderevaluation"] = selectedEval;
    }
    else
    {
      opportunity["new_colderevaluation"] = null;
    }