web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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);

I have the same question (0)
  • Verified answer
    Pankaj Gogoi Profile Picture
    3,177 on at

    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

  • Jamal JEK Profile Picture
    35 on at

    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

    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

  • Verified answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    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

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
11manish Profile Picture

11manish 176

#2
ManoVerse Profile Picture

ManoVerse 56 Super User 2026 Season 1

#3
Niki Patel Profile Picture

Niki Patel 42

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans