Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Answered

Dataverse Plugin Exception: Object reference not set to an instance of an object.

Posted on by 10

Hello,

1. I know this question shows up a lot and has been answered quite often.  I've looked through the reponses and so far none of them have helped.

2. I am relatively new to developing plugins for dataverse, so parden any "newbie" silly errors.

I'm attempting to update an entity based on a calculated date. When running the plugin in dataverse without profiling I'm getting the error "Object reference not set to an instance of an object." When I try and debug this error using the profiler I get an error that simply states to download the error file. When attempting to profile using 'Persist to Entity' option I'm getting an "Unable to persist the profile." error message and the profile doesn't save for debugging.

"

Exception Message: Object reference not set to an instance of an object.

ErrorCode: -2147220956
HexErrorCode: 0x80040224

HelpLink: go.microsoft.com/.../

TraceText:
[CreateProgramStartSchedule: CreateProgramStartSchedule.CreateProgramSched
[fd550148-3572-ed11-9561-00224805caa9: CreateProgramStartSchedule.CreateProgramSched: Update of pt_programstarts

Activity Id: 7e06306d-995a-4954-96d5-83c6acfe707a

"

pastedimage1669985412539v5.png

I know this error usually indicates that one of the values I'm trying to update is null, but this doesn't seem to be the case.

In the code below the error is occuring on "service.Update(ProgramStart);" regardless of which version of the code I run.

The code that is commented out is different variations that I've tried.

if (WorkingHours >= (SessionNumber * SessionHours))
{
  SessionEnd = WorkingDate;
  if (SessionNumber == 1)
  {
  // save value as end of first session of this start

  /*
  NextStartFlag = true;
  object[] EndDateField = {"pt_enddate", WorkingDate };
  UpdateEntity("pt_programstarts", ProgramStart.Id, EndDateField, service);
  */

  /*
  Entity UpdateProgramStart = new Entity("pt_programstarts", ProgramStart.Id);
  UpdateProgramStart.Attributes.Add("pt_enddate", WorkingDate);
  service.Update(UpdateProgramStart);
  */

  ProgramStart.Attributes.Add("pt_enddate", WorkingDate);
  service.Update(ProgramStart);
  }

"pt_programstarts" is the correct logical name for the entity.

pastedimage1669984464037v1.png

"pt_enddate" is the correct name for the field.

pastedimage1669984507397v2.png

The Variable "WorkingDate" does have a value.

pastedimage1669984562426v3.png

The entity "pt_programstarts" has a valid Guid and seems to be a valid entity.

pastedimage1669984604225v4.png

Just for complete information..

pastedimage1669986052290v6.png

private void UpdateEntity(string EntityName, Guid EntityGuid, object[] Field, IOrganizationService service)
{
    Entity UpdateEntity = new Entity(EntityName, EntityGuid);
    for(var i = 0; i < Field.Length; i  )
    {
        string FieldName = Convert.ToString(Field[i]);
        i  ;
        object FieldValue = Field[i];
        UpdateEntity.Attributes.Add(FieldName, FieldValue);
    }
    service.Update(UpdateEntity);
}

Any ideas what I'm missing or what object isn't set?

  • Gene_B Profile Picture
    Gene_B 10 on at
    RE: Dataverse Plugin Exception: Object reference not set to an instance of an object.

    I'm using a Post-op.

    I've updated the code to

    entity["pt_enddate"] = WorkingDate;
     

    (where "entity" was set early to the context target). This seems to do the trick. I guess it didn't like creating a new entity to update the current entity?  But it seems to work now.  Thank you.

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Dataverse Plugin Exception: Object reference not set to an instance of an object.

    let's simplify a bit. Between all the code you posted, the one that makes more sense is this one:

    Entity UpdateProgramStart = new Entity("pt_programstarts", ProgramStart.Id);
    UpdateProgramStart.Attributes.Add("pt_enddate", WorkingDate);
    service.Update(UpdateProgramStart);

    because the method UpdateEntity passing an object array with first the logicalname and after the value is something I would not use.

    First I will rewrite your code in this way:

    Entity UpdateProgramStart = new Entity("pt_programstarts", ProgramStart.Id);
    UpdateProgramStart["pt_enddate"] = WorkingDate;
    service.Update(UpdateProgramStart);

    now, assuming (as per your screenshots) ProgramStart.Id is a Guid of the record you want to update and WorkingDate is a DateTime object, the code works.

    Because you are in a plugin an Update to the same record is done when you are in post-op sync or inside an async. Which pipeline you are?

    Because pre-op sync you can update the current record without using Update, just setting the values to the Target object.

    Also you can create a small console application to test just the update code part.

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans