Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

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

(0) ShareShare
ReportReport
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
    28,979 Moderator 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
    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
    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
    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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,325 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,025 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans