Error creating an object in CRM 2011 - CRM does not like OptionSetValue

I am trying to create an entity in CRM 2011 (not from view, but what in CRM 4 could be called DynamicEntity ... with my custom attributes). The code below gives me this error, and I'm not sure why. This exact code works if I remove the new_accounttype attribute and try to use a different custom attribute.

CRM seems to have decided that the OptionSetValue parameter is set as the value for this key value pair. new_accounttype is a select list (or OptionSet in CRM 2011), and the value 100000003 been pulled from the front end to be valid.

Error: A validation error has occurred. The value "new_accounttype" in the account type is out of 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); } } 
+4
source share
2 answers

I agree that what you need should work fine. This can only mean that the value is not published or incorrect. As @glosrob mentions, check if the changes are actually posted. Confirm these values ​​by looking at the published form and see if your new value is present (and maybe double check with IE Developer Tools - press F12) and make sure that the value in the select> option object in HTML contains the integer that you expect )

As an aside, your code looks more complex than necessary (IMHO!). I find it easier to read no less efficiently:

Try the following:

 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); } } 
+3
source

Take a picture: key = "new_accounttype", value = new OptionSetValue(100000003)

0
source

All Articles