Answered by:
create appointment, unable to set RegardingObjectId property

Question
-
I have an application which creates and manages leave requests. I want to be able to populate the standard calendars with information about these leave requests. So I do;
ActivityParty who = new ActivityParty() { PartyId = new EntityReference(SystemUser.EntityLogicalName, lr.widget_leave_employee.Id), }; Appointment app = new Appointment() { Subject = lr.widget_leave_type.Name, ScheduledStart = lr.widget_leave_Startdate.Value.Date.AddHours(utilities.GetBool(lr.widget_Leave_StartAM2, true) ? 0 : 12), ScheduledEnd = lr.widget_leave_Enddate.Value.Date.AddHours(utilities.GetBool(lr.widget_Leave_EndPM2, true) ? 24 : 12), OwnerId = new EntityReference() { Id = lr.widget_leave_employee.Id, LogicalName = SystemUser.EntityLogicalName, }, RequiredAttendees = new ActivityParty[] { who }, }; Guid appid = service.Create(app);
Which is all well and guid and it works. I also want to reference the leave request from the appointment. This is so that if leave requests are changed I can find the corresponding appointment and update it. So I try;
ActivityParty who = new ActivityParty() { PartyId = new EntityReference(SystemUser.EntityLogicalName, lr.widget_leave_employee.Id), }; Appointment app = new Appointment() { Subject = lr.widget_leave_type.Name, ScheduledStart = lr.widget_leave_Startdate.Value.Date.AddHours(utilities.GetBool(lr.widget_Leave_StartAM2, true) ? 0 : 12), ScheduledEnd = lr.widget_leave_Enddate.Value.Date.AddHours(utilities.GetBool(lr.widget_Leave_EndPM2, true) ? 24 : 12), OwnerId = new EntityReference() { Id = lr.widget_leave_employee.Id, LogicalName = SystemUser.EntityLogicalName, }, RegardingObjectId = new EntityReference { Id = lr.Id, LogicalName = widget_leaverequest.EntityLogicalName, }, RequiredAttendees = new ActivityParty[] { who }, }; Guid appid = service.Create(app);
This now fails with the message about an invalid parent referring to the RegardingObjectId field. So what is this complaining about. Is an appointment unable to refer to a custom entity or am I doing something wrong. I could create a field in my custom entity to hold a reference to the appointment but I was hoping to avoid needing to write back to the custom entity again.
Tuesday, November 5, 2013 3:54 PM
Answers
-
The code looks OK. Is the widget_leaverequest entity configured to link to activities ?
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
- Marked as answer by J N Brand Wednesday, November 6, 2013 11:07 AM
Wednesday, November 6, 2013 10:24 AMModerator
All replies
-
Your RegardingObjectId is set based on properties of the lr and widget_leaverequest variables, but you haven't shown how these are instantiated. I expect it is a problem with one or other of these variables
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
Tuesday, November 5, 2013 4:43 PMModerator -
This is invoked in a post create plug in and the lr entity is the entity that was created and passed into that plugin;
var entity = context.InputParameters["Target"] as Entity; var entity2 = service.Retrieve(widget_leaverequest.EntityLogicalName, entity.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true)); if (entity != null) { // NB this works in create but in update most of the parameters are null! widget_leaverequest lr = entity2.ToEntity<widget_leaverequest>();
the lr entity is used in other operations so it has been instantiated. widget_leaverequest is my custom entity and corresponds to a class exported from the CRM system containing it.
Tuesday, November 5, 2013 4:51 PM -
The code looks OK. Is the widget_leaverequest entity configured to link to activities ?
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
- Marked as answer by J N Brand Wednesday, November 6, 2013 11:07 AM
Wednesday, November 6, 2013 10:24 AMModerator -
The code looks OK. Is the widget_leaverequest entity configured to link to activities ?
No it wasn't! Thanks for pointing that out. I enabled it and suddenly everything works.
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk
Wednesday, November 6, 2013 11:06 AM