开发者

Update attribute with NULL

开发者 https://www.devze.com 2023-04-01 02:37 出处:网络
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

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(rblC开发者_StackOverflow中文版hangeButtonType.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?


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


You can set the attribute to null, like so :

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


I have added a few simple methods to my CrmUtil static class which I use quite a lot. These may be helpful.

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

Alternatively, you could change these to be extention methods for either Entity or AttributeCollection, like so

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

精彩评论

暂无评论...
验证码 换一张
取 消