1
votes

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".

4
I took the liberty of heavily downsize your code to illustrate the essential part of the problem. Hope you like it. - Konrad Viltersten
is better you do a simple var value = entity["abc_yesNoField"]; to retrieve the real field value, try to throw the execption with this variable - Guido Preite
I bet that if you intentionally misspell the name of the field, you'll get the same behavior. Check it, please. Also, could you line-brake the examples a little better, please? - Konrad Viltersten
Hi, I did try my best to format my question.Thanks Guido, I did follow your idea of throwing an exception to see what value i was getting, and I was getting "false" even if "Yes" was chosen. - Jaya

4 Answers

1
votes

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

1
votes

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) { ... }
1
votes

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");
1
votes

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)