Hi all. I need to check attributes in an entity but they could exist in the target if it was changed during the transaction or on the Pre Business Entity if it existed before but didn't change. Example if Name was "Alex" but changed to "Alexander" I'd like to have "Alexander", but if it hadn't changed I'd like to get "Alex". Or if it wasn't set at all I'd like the variable to just be empty or null. Here's what I have now:
public object GetValue (Entity entity1, Entity entity2, string Attribute) { object Output = new object(); if (entity1.Attributes.ContainsKey(Attribute)) Output = entity1[Attribute]; else if (entity2.Attributes.ContainsKey(Attribute)) Output = entity2[Attribute]; return Output; }
This works great if the value exists one or the other entities:
EntityReference Account = (EntityReference)GetValue(PostChange, PreChange, "primarycontactid")
But if the Attribute is null in either case this errors since it can't cast Object to whatever type I'm passing back. Can someone give suggestions on how to get past this? I'd love for the GetValue method to work whether or not the value exists which is the point in such a method, but I can't get past this.
Thanks for any suggestions --