Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

How to fix : Object reference not set to an instance of an object?

Posted on by 35

Hi, i am using ParentId to  retrieve only the records of the current account of a customer address in a plugin,

it worked fine when the plugin was triggered on the create of a customer address, but when i tried it in update i get the error :

Error of Plugin: System.NullReferenceException: Object reference not set to an instance of an object. can anyone help ?

                        var addresstype = entity.GetAttributeValue("addresstypecode").Value;
                        tracingService.Trace("Addresstype value = "   addresstype);
                        var accountid = entity.GetAttributeValue("parentid").Id;
                        tracingService.Trace("ACcount value = "   accountid);
                        QueryExpression query = new QueryExpression("customeraddress");
                        query.ColumnSet = new ColumnSet("addresstypecode");
                        query.Criteria.FilterOperator = LogicalOperator.And;
                        query.Criteria.AddCondition("addresstypecode", ConditionOperator.Equal, addresstype);
                        query.Criteria.AddCondition("parentid", ConditionOperator.Equal, accountid);
                        EntityCollection collection = service.RetrieveMultiple(query);

  • Verified answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: How to fix : Object reference not set to an instance of an object?

    Hi,

    You should always check null condition even for create message.

    var accountid=Guid.Empty();

    EntityReference erfparentid=entity.GetAttributeValue<EntityReference>("parentid");

    if(erfparentid!=null)

    {

    accountid=erfparentid.Id;

    }

    Add below logic only when MessageName is update because your existing code is already working for create.

    As suggested by Pankaj, you can register image for update stage.

    But, you should first check if Target has this field data then use that if not take value from image.

    If you directly take value from image, and if use update this field on form you will only get old value not latest value.

    Please mark my answer verified if i were helpful

  • Verified answer
    Pankaj Gogoi Profile Picture
    Pankaj Gogoi 3,177 on at
    RE: How to fix : Object reference not set to an instance of an object?

    Hi Jamal,

    Since parentid field is not updated, it will not be part of the Target Entity. You can us a retrieve operatio to fetch the parentid of the current record but this will be an additional load or you can register a pre-image with the update plugin registration process, and you don't need to call retieve and you can get attribute value from pre-entity.

    var preEntity = (Entity)context.PreEntityImages[preimagename];

    Best Regards

    PG

  • Jamal JEK Profile Picture
    Jamal JEK 35 on at
    RE: How to fix : Object reference not set to an instance of an object?

    Thank you Pankaj for your reply

    The line 3 is the one is throwing the error

        var accountid = entity.GetAttributeValue<EntityReference>("parentid").Id;

    I need to compare the addresstypecode of the updated record and the addresstypecode of the others records, not the  addresstypecode of the updated and the old record

    Best Regards

  • Verified answer
    Pankaj Gogoi Profile Picture
    Pankaj Gogoi 3,177 on at
    RE: How to fix : Object reference not set to an instance of an object?

    Which line is throwing the error? You are trying to get Value from an object that is null. Either addresstypecode or parentid is null.

    On update the entity will contain only the modified values being modified in the current pipeline.

    You can use PostEntityImages

    https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/register-plug-in#types-of-entity-images

    https://community.dynamics.com/crm/b/thinkdynamicdodynamic/posts/pre-image-and-post-image-in-dynamics-crm

    Best Regards

    PG

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans