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
"
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.
"pt_enddate" is the correct name for the field.
The Variable "WorkingDate" does have a value.
The entity "pt_programstarts" has a valid Guid and seems to be a valid entity.
Just for complete information..
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?
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.
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.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156