locked
Problem Iterating ServiceAppointment.Resources RRS feed

  • Question

  • I get the error message "

    Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'ActivityParty'." when attempting to interate through the ServiceAppointment.Resources field.

    See line in code at the bottom below. What am I doing wrong?

    public class ServiceActivityTechScheduling: Plugin { public ServiceActivityTechScheduling() : base(typeof(ServiceActivityTechScheduling)) { base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Create", "serviceappointment", new Action<LocalPluginContext>(ExecuteServiceActivityTechScheduling))); base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "serviceappointment", new Action<LocalPluginContext>(ExecuteServiceActivityTechScheduling))); base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Delete", "serviceappointment", new Action<LocalPluginContext>(ExecuteServiceActivityTechScheduling))); } protected void ExecuteServiceActivityTechScheduling(LocalPluginContext localContext) { string postImageAlias = "PostImage"; string preImageAlias = "PreImage"; Guid InitiatingUserGuid = Guid.Empty; string sInitiatingUser = ""; try { if (localContext == null) { throw new ArgumentNullException("localContext is null"); } IPluginExecutionContext context = localContext.PluginExecutionContext; if (context.Depth > 9) { return; } InitiatingUserGuid = context.InitiatingUserId; Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(postImageAlias)) ? context.PostEntityImages[postImageAlias] : null; Entity preImageEntity = (context.PreEntityImages != null && context.PreEntityImages.Contains(preImageAlias)) ? context.PreEntityImages[preImageAlias] : null; Guid currUser = context.UserId; string serverAddr = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority); string orgName = context.OrganizationName; ServiceHelper svcHelper = new ServiceHelper(); using (OrganizationServiceProxy proxy = svcHelper.CreateOrganizationService(serverAddr, context.OrganizationName, context.UserId)) { proxy.EnableProxyTypes(); ServiceAppointment sa = null; SalesOrder so = null; SalesOrderDetail sod = null; if (context.MessageName == "Delete") { sa = preImageEntity.ToEntity<ServiceAppointment>(); if (sa == null) { throw new Exception("Unable to retrieve ServiceAppointment record"); } } else { sa = postImageEntity.ToEntity<ServiceAppointment>(); if (sa == null) { throw new Exception("Unable to retrieve ServiceAppointment record"); } } Guid sodGuid; if (Guid.TryParse(sa.Subcategory, out sodGuid)) { sod = proxy.Retrieve(SalesOrderDetail.EntityLogicalName, sodGuid, new ColumnSet(true)).ToEntity<SalesOrderDetail>(); if (sod != null) { so = proxy.Retrieve(SalesOrder.EntityLogicalName, sod.SalesOrderId.Id, new ColumnSet(true)).ToEntity<SalesOrder>(); if (so != null) { if (context.MessageName != "Delete") { foreach(ActivityParty ap in sa.Resources) <-- *** Error Occurs Here *** { if (ap.PartyId.LogicalName.Equals(SystemUser.EntityLogicalName)) { ...


    Thursday, October 10, 2013 10:59 AM

Answers

  • That didn't work David. I received the same error as before on the foreach statement.

    I found the solution at www. stackoverflow.com/a/8165587. I implemented the same methodology in my code and it worked.

    Monday, October 14, 2013 9:49 PM

All replies

  • Try using the following instead:

    foreach(Entity ap in sa.Resources)

    { if (((EntityReference)ap["PartyId"]).LogicalName.Equals(SystemUser.EntityLogicalName))



    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Thursday, October 10, 2013 1:10 PM
    Moderator
  • That didn't work David. I received the same error as before on the foreach statement.

    I found the solution at www. stackoverflow.com/a/8165587. I implemented the same methodology in my code and it worked.

    Monday, October 14, 2013 9:49 PM