问题
Am trying to update child contacts based on a "Two Options" field in the account form. I update all child contacts to the updated address of parent account if the "Two Option" field is set to "Yes". I try to retrieve the value using the following code,
bool? updateContactsValue
= entity.GetAttributeValue<bool?>("abc_yesNoField");
if (updateContactsValue)
{
String[] details = new string[7];
String telephoneNum = String.Empty, ... , country = String.Empty;
telephoneNum = entity.GetAttributeValue<String>("telephone1");
details[0] = telephoneNum;
...
UpdateContact(service, entity.Id, details);
}
But i find that the address fields are not being updated even if the selected option is "Yes". Am i missing something here?
Hi all, I have modified the code to as follows
bool? updateContactsValue=null;
updateContactsValue = entity.GetAttributeValue<bool?> ("abc_yesNoField").GetValueOrDefault();
throw new Exception( "My custom Exception"+" "+entity.GetAttributeValue<bool?>("abc_yesNoField").GetValueOrDefault().ToString());
The system throws "false" even if I had chosen "Yes".
回答1:
Probably you wrote wrong your field name, it can't be "updatechildcontacts". Because is a custom field it must starts with a prefix, like new_updatechildcontacts
回答2:
It's just a shot from the hip, because I'm not at the computer right now, but I'm fairly sure that you (for some reason that I can't tell right now), fail to fetch the value and get the bool to be null. And the execution of the update is conditioned by it being true.
You need to check the value of the field in a different way.
I'm not sure but I'd expect the yes/no element to be a bool and not a nullable bool. What happens if you go like this? Does it compile?
bool updateContactsValue
= entity.GetAttributeValue<bool>("abc_yesNoField");
if (updateContactsValue) { ... }
回答3:
The error is in the query statement, please make sure that you have inserted your attribute inside it. If it is not inserted it will always give you false value for example:
QueryExpression query = new QueryExpression("ntw_abc");
query.ColumnSet = new ColumnSet("ntw_referencenumber", "abc_yesNoField");
回答4:
May be problem is due to that you are using Scheema Name i.e mixed case. abc_yesNoField cannot be Name of attribute bcoz it uses mixed case. It should be LowerCase like abc_yesnofield
bool? updateContactsValue= entity.GetAttributeValue<bool?>("abc_yesnofield");
Always Use Logical Name of attribute i.e Name (Always lowercase) in:
1. Xrm
2. c# code late bind
Use Scheema Name of attribute i.e (mixed case or camel casing) in:
1. REST/oData/JSON object model
2. c# early bound classes (LINQ)
来源:https://stackoverflow.com/questions/15692419/retrieving-two-option-field-value-on-the-plugin-in-crm-2011