编程语言
首页 > 编程语言> > c# – 在CRM 2011中创建实体时出错 – CRM不喜欢OptionSetValue

c# – 在CRM 2011中创建实体时出错 – CRM不喜欢OptionSetValue

作者:互联网

我正在尝试在CRM 2011中创建一个实体(不是开箱即用的类型,但CRM 4中的内容将被称为DynamicEntity …具有我的自定义属性).下面的代码给了我这个错误,我不知道为什么.如果我删除new_accounttype属性并尝试使用其他自定义属性,则完全相同的代码可以正常工作.

CRM似乎已经将“OptionSetValue”设置为该键值对的值. new_accounttype是一个选项列表(或CRM 2011中的OptionSet),并且从前端提取了100000003的值,因此它是一个有效值.

Error: A validation error occurred. The value of ‘new_accounttype’ on
record of type ‘account’ is outside the valid range.

我究竟做错了什么?

public static void CreateAccount(string accountName, string accountType)
{
   //Create properties
   KeyValuePairOfstringanyType[] attributes = new KeyValuePairOfstringanyType[2];
   attributes[0] = new KeyValuePairOfstringanyType() { key = "name", value = accountName ?? "" };
   attributes[1] = new KeyValuePairOfstringanyType() { key = "new_accounttype", value = new OptionSetValue() { Value = 100000003 } };

   ////Create DynamicEntity
   Entity accountToCreate = new Entity();
   accountToCreate.LogicalName = "account";
   accountToCreate.Attributes = attributes;

   try
   {
     service.Create(accountToCreate);
   }
}

解决方法:

我同意你所拥有的应该工作得很好.这只能意味着该值未发布或不正确.正如@glosrob提到的那样,检查更改是否实际发布.通过查看已发布的表单并查看是否存在新值来确认这些值(也许可以使用IE Developer Tools进行双重检查 – 点击F12 – 并确认HTML中select>选项对象中的值包含您期望的整数).

顺便说一句,你的代码看起来比必要的复杂(恕我直言!).我相信这更容易阅读效率不低:

试试这个:

public static void CreateAccount(string accountName, string accountType)
{
   ////Create DynamicEntity
   Entity accountToCreate = new Entity();
   accountToCreate.LogicalName = "account";
   accountToCreate.Attributes = attributes;

   //Append properties
   accountToCreate.Attributes.Add("name", accountName ?? "" );
   accountToCreate.Attributes.Add("new_accounttype", new OptionSetValue(100000003);

   try
   {
     service.Create(accountToCreate);
   }
}

标签:c,dynamics-crm,dynamics-crm-2011
来源: https://codeday.me/bug/20190630/1337797.html