Hey Alex,
A little background on my problem actually. THere are three entities which are involved in my problem. They are all OOB entities
- recurringappointmentmaster, appointment and serviceappointment. My end goal is when a user deletes a recurringappointmentmaster
record, the corresponding serviceappointment records' status must be flipped to cancelled. I hope you're familiar with how recurringappointmentmaster works
and how they're associated with appointment. If not let me know , I can tell you. Anyways, now I made some modifications to the code- (Getting the requiredattendees from preimage of appointment record) . And made some modifications to the code as to where it is failing. Please find below code and the tracelog. So it looks like the system is not getting the requiredattendees data ?
Code:
using DevPlugIns.Common;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
namespace DevPlugIns
{
public class ChangeStatusofServiceActivity : IPlugin
{
const string PreImage = "PreImage";
//running on appointment entity
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService _service = factory.CreateOrganizationService(context.InitiatingUserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.InputParameters != null)
{
tracingService.Trace("Entering method... ");
Entity localAppointment = new Entity("appointment", context.PrimaryEntityId);
Entity preImageAppointmentRecord = context.PreEntityImages[PreImage];
tracingService.Trace("Appointment Record & PreImage Retrieved ");
String appSubject = preImageAppointmentRecord.GetAttributeValue<String>(Constants.Appointment.Subject); //schemaname = "subject"
tracingService.Trace("Subject of appointment retrieved ");
EntityCollection partyList = preImageAppointmentRecord.GetAttributeValue<EntityCollection>(Constants.Appointment.RequiredAttendees); //SN: "requiredattendees";
foreach (Entity resourceList in partyList.Entities)
{
tracingService.Trace("The list of resources from appointment record is retrieved." + resourceList.GetAttributeValue<EntityReference>("partyid").Id);
}
EntityCollection collectionOfServiceActivitites = _service.RetrieveMultiple((new FetchExpression(string.Format(Constants.FetchXmls.ServiceActivitiesFetch, appSubject))));
//Retrieving Service activities where the subject name is equal, the subject field, scheduled start, scheduled end is retrieved from service activities is retrieved.
if (collectionOfServiceActivitites.Entities.Count > 0)
{
foreach (Entity serviceactivity in collectionOfServiceActivitites.Entities)
{
EntityCollection activityResources = serviceactivity.GetAttributeValue<EntityCollection>(Constants.ServiceActivity.Resources);
tracingService.Trace("Activity resources Collected" + activityResources);
Entity foundParty = null;
foreach (Entity activityResource in activityResources.Entities)
{
tracingService.Trace("Into the method to CHECK 2 ");
var ResourceId = activityResource.GetAttributeValue<EntityReference>("partyid").Id;
tracingService.Trace("Resource ID received" + ResourceId);
foundParty = partyList.Entities.ToList().FirstOrDefault(party => (party.GetAttributeValue<EntityReference>("partyid")).Id == ResourceId);
if (foundParty != null)
{
tracingService.Trace("It is into method of party list");
DateTime serActEndField = serviceactivity.GetAttributeValue<DateTime>(Constants.ServiceActivity.ScheduledEnd).ToUniversalTime();
String serActEndTime = serActEndField.ToString("t");
tracingService.Trace("Service Activity End Time : " + serActEndTime);
tracingService.Trace("About to check conditions to validate EC ");
if (serviceactivity.GetAttributeValue<EntityReference>(Constants.ServiceActivity.EC) == localAppointment.GetAttributeValue<EntityReference>(Constants.Appointment.EC))
{
tracingService.Trace("Entering method to update values");
CancelServiceActivity(_service, serviceactivity.ToEntityReference());
}
else
{
tracingService.Trace("Failed Center Check");
}
break;
}
else
{
tracingService.Trace("failed method");
}
}
}
// _service.Update(localAppointment);
}
}
}
private void CancelServiceActivity(IOrganizationService _service, EntityReference entityReference)
{
SetStateRequest appointmentRequest = new SetStateRequest
{
State = new OptionSetValue(2),
Status = new OptionSetValue(9),
EntityMoniker = entityReference
};
_service.Execute(appointmentRequest);
}
}
}
Trace:
Entering method...
Appointment Record & PreImage Retrieved
Subject of appointment retrieved
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9c8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9c8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9c8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9c8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9c8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9c8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9c8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9c8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9c8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9c8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9e8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9e8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9e8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9e8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9e8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9e8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9e8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9e8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9e8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9e8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9a8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9a8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9a8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9a8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9a8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9a8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9a8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9a8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9a8d6909-1860-e611-80c5-005056aa8521
failed method
Activity resources CollectedMicrosoft.Xrm.Sdk.EntityCollection
Into the method to CHECK 2
Resource ID received9a8d6909-1860-e611-80c5-005056aa8521
failed method