Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Getting value from either Target entity or PreBusiness entity if it exists in SDK Plugin - need some suggestions

Posted on by Microsoft Employee

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

  • shivaram Profile Picture
    shivaram 3,315 on at
    RE: Getting value from either Target entity or PreBusiness entity if it exists in SDK Plugin - need some suggestions

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

  • Tim Dutcher Profile Picture
    Tim Dutcher 2,100 on at
    RE: Getting value from either Target entity or PreBusiness entity if it exists in SDK Plugin - need some suggestions

    Try checking for null in your IF statements.

    if (entity1.Attributes.ContainsKey(Attribute) && entity1[Attribute] != null)

  • Inogic Profile Picture
    Inogic 24,094 on at
    RE: Getting value from either Target entity or PreBusiness entity if it exists in SDK Plugin - need some suggestions

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Getting value from either Target entity or PreBusiness entity if it exists in SDK Plugin - need some suggestions

    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.

  • Suggested answer
    Mahadeo Matre Profile Picture
    Mahadeo Matre 17,021 on at
    RE: Getting value from either Target entity or PreBusiness entity if it exists in SDK Plugin - need some suggestions

    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;

           }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans