使用NULL更新属性

时间:2011-08-31 03:01:06

标签: c# dynamics-crm-2011 dynamics-crm

我正在尝试更新一个OptionSetValue实体,它将更新的值在radioboxlist中,其中一个选项为blank / null。有没有办法将空值传递给CRM实体进行更新?

这是radiobox列表:

<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>

这是我用来更新实体的代码,它适用于所有值,但空白值。

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

OptionSetValue selectedEval = new OptionSetValue(colderEval);

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

还知道如何传递空白吗?

3 个答案:

答案 0 :(得分:2)

只需检查您的特殊空白值,然后在必要时将该字段设置为null:

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;
}

答案 1 :(得分:1)

您可以将属性设置为null,如下所示:

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

答案 2 :(得分:-1)

我在CrmUtil静态类中添加了一些简单的方法,我使用了很多。这些可能会有所帮助。

public static class CrmUtils
{
    public static OptionSetValue OptionSetValueOrNull(int? value)
    {
        return value == null ? null : new OptionSetValue(value);
    }

    public static EntityReference EntityReferenceOrNull(Guid? id, string entityName)
    {
        return id == null ? null : new EntityReference(entityName, id);
    }
}

或者,您可以将这些更改为Entity或AttributeCollection的扩展方法,如此

entity.SetEntityReference(nullableId, "account");
entity.Attributes.SetEntityReference(nullableId, "account");