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 --
*This post is locked for comments
Hi Samlex,
This is simple. You need to follow Mahadeo suggestion and simply you can check null condition where you are calling this method.
For ex
var op=GetValue(account,contact,new_name);
if(op!=null){
// your operation
}
else
{
throw new invalidpluginexception("Related field value is null")
}
Because if value is not change then definitely it will throw Null Exception(Object reference is not set to........)
Try checking for null in your IF statements.
if (entity1.Attributes.ContainsKey(Attribute) && entity1[Attribute] != null)
Hi Samalex,
The error occurred because you are trying to return “object” and set it to the “Entity Reference”. It will work when record contains data but will fail if it doesnot contain data.
So you can simply return null if it does not contain data. And as you are saying that it is giving Object Reference issue then it may be because you are using the EntityReference object further for processing. So before using that you will need to check whether the Entity reference object returned is not null.
Let us know if you are still facing the issue.
Thanks!
Sam
I tried that before, but it won't allow me to set a null object as Entity Reference. I get the error "Object reference not set to an instance of an object" when I do this. If I were to set it to New EntityReference() it would work, but this would require me to overload GetValue for every possible object type.
This just may not be possible. I hoped I could get it going somehow just so I could cut back the amount of code since in most cases I have to check the New values then Old values so I'm using the newest data.
Thanks for your feedback.
try
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];
else
Output = null;
return Output;
}
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156