在 CRM 2011 中创建实体时出错 - CRM 不喜欢 OptionSetValue
我正在尝试在 CRM 2011 中创建一个实体(不是开箱即用的实体,而是在 CRM 4 中称为 DynamicEntity...具有我的自定义属性的实体)。下面的代码给了我这个错误,我不知道为什么。如果我删除 new_accounttype
属性并尝试使用另一个自定义属性,则此完全相同的代码将有效。
CRM 似乎对将“OptionSetValue”设置为该键值对的值存在疑问。 new_accounttype 是一个选项列表(或 CRM 2011 中的 OptionSet),并且值 100000003
是从前端提取的,因此它是有效值。
错误:发生验证错误。 'new_accounttype' 的值 “帐户”类型的记录超出了有效范围。
我做错了什么?
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);
}
}
I'm trying to create an entity in CRM 2011 (not an out of the box kind, but what in CRM 4 would have been called a DynamicEntity... one with my custom attributes). The code below gives me this error and I'm not sure why. This exact same code works if I remove the new_accounttype
attribute and try to use another custom attribute.
CRM seems to have taken issue with the "OptionSetValue" being set as the value for that key value pair. new_accounttype is a picklist (or OptionSet in CRM 2011) and that value of 100000003
was pulled from the front end so it's a valid value.
Error: A validation error occurred. The value of 'new_accounttype' on
record of type 'account' is outside the valid range.
What am I doing wrong?
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我同意你所拥有的应该可以正常工作。这只能意味着该值未发布或不正确。正如 @glosrob 提到的,检查更改是否实际发布。通过查看已发布的表单并查看是否存在新值来确认这些值(也许可以使用 IE 开发工具进行双重检查 - 按 F12 - 并确认 HTML 中的 select>option 对象中的值包含您期望的整数)。
顺便说一句,您的代码看起来比必要的更复杂(恕我直言!)。我相信这更容易阅读且效率同样不低:
试试这个:
I agree that what you have should work fine. This can only mean that the value isn't published or is incorrect. As @glosrob mentions, check that the changes are actually published. Confirm these values by looking at the published form and seeing if your new value is present (and perhaps double check by using IE Developer Tools - hit F12 - and confirm that the value in the select>option object in the HTML contains the integer you expect).
As an aside, your code looks more complex than necessary (IMHO!). I believe this is easier to read an no less efficient:
Try this:
尝试一下:
key = "new_accounttype", value = new OptionSetValue(100000003)
Give this a shot:
key = "new_accounttype", value = new OptionSetValue(100000003)