My Plugin
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace CFM.UpdateCustomerAsset.BookingDate
{
public class SuppressionBookingDates : IPlugin
{
IOrganizationService service = null;
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName == "msdyn_agreementbookingdate")
{
// tracingService.Trace("AdvancedPlugin: Verifying the client is not offline.");
tracingService.Trace("Plugin Entered Agreementbookingdate");
if (entity.Contains("msdyn_agreement"))
{
//4
tracingService.Trace("Entered Agreement.");
if ((entity.Attributes["msdyn_agreement"]) != null)
{
//5
Guid agreement = ((EntityReference)entity.Attributes["msdyn_agreement"]).Id;
// get booking dates with the same agreement and does not have the same id as the entity
var fetchXmlDates = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
fetchXmlDates += "<entity name='msdyn_agreementbookingdate'>";
fetchXmlDates += "<attribute name='msdyn_bookingsetup' />";
fetchXmlDates += "<attribute name='msdyn_bookingdate' />";
fetchXmlDates += "<attribute name='msdyn_agreement' />";
fetchXmlDates += "<attribute name='msdyn_agreementbookingdateid' />";
fetchXmlDates += "<order attribute='msdyn_bookingdate' descending='true' />";
fetchXmlDates += "<filter type='and'>";
fetchXmlDates += "<condition attribute='msdyn_agreement' operator='eq' uitype='msdyn_agreement' value='" + agreement + "' />";
fetchXmlDates += "<condition attribute='msdyn_agreementbookingdateid' operator='ne' value='" + entity.Id + "' />";
fetchXmlDates += "</filter>";
fetchXmlDates += "<link-entity name='msdyn_agreementbookingsetup' from='msdyn_agreementbookingsetupid' to='msdyn_bookingsetup' alias='bookRec'>";
fetchXmlDates += "<attribute name='new_bookingrecurring' />";
fetchXmlDates += "<attribute name='new_customerasset' />";
fetchXmlDates += "<filter type='and'>";
fetchXmlDates += "<condition attribute='new_bookingrecurring' operator='not-null' />";
fetchXmlDates += "</filter>";
fetchXmlDates += "</link-entity>";
fetchXmlDates += "</entity>";
fetchXmlDates += "</fetch>";
var resultDates = service.RetrieveMultiple(new FetchExpression(fetchXmlDates));
tracingService.Trace("Completed FetchXml");
try
{
tracingService.Trace("Entered Try block");
if (resultDates.Entities.Count > 1)
{
foreach (Entity bookingDate in resultDates.Entities)
{
tracingService.Trace("Booking Date", bookingDate.Id);
if (entity.Contains("msdyn_bookingdate"))
{
// check the booking value option set
OptionSetValue RelatEntityBookingRec = GetRelatedAtt(bookingDate);
OptionSetValue entityBookingRec = GetRelatedAtt(entity);
tracingService.Trace("Optionset RelateentitybookingRec");
// get the numbered values to compare between fields and suppress dates
var entityBookNum = entityBookingRec.Value;
var fetchBookNum = RelatEntityBookingRec.Value;
// get month of the date to compare
var bookingFetchDate = (DateTime)bookingDate.Attributes["msdyn_bookingdate"];
var entityDate = (DateTime)entity.Attributes["msdyn_bookingdate"];
var bookingFetchCA = "";
var entityCA = "";
tracingService.Trace("BookingFetch CA");
bookingFetchCA = bookingDate.Attributes.Contains("new_customerasset") ? (string)bookingDate.Attributes["new_customerasset"] : String.Empty;
entityCA = entity.Attributes.Contains("new_customerasset") ? (string)entity.Attributes["new_customerasset"] : String.Empty;
tracingService.Trace("entity CA");
if (entity.Id != bookingDate.Id)
{
if (bookingFetchDate == entityDate)// && bookingFetchCA == "84f07501-9b39-e911-a960-000d3a4647a5")
{
if (bookingFetchCA == entityCA)
{
foreach (Entity item in resultDates.Entities)
{
if (entityBookNum < fetchBookNum)
{
// delete the current entity
var newEntity = new EntityReference(entity.LogicalName, entity.Id);
service.Delete(entity.LogicalName, entity.Id);
}
else if (entityBookNum > fetchBookNum)
{
// delete query
var newEntity = new EntityReference(bookingDate.LogicalName, bookingDate.Id);
service.Delete(newEntity.LogicalName, newEntity.Id);
}
}
}
}
}
}
}
}
}
catch (Exception e)
{
throw new InvalidPluginExecutionException(e.ToString());
}
}
}
}
}
}
// method to retrieve the attribute from related entity
private OptionSetValue GetRelatedAtt(Entity target)
{
OptionSetValue att = null;
// check entity is (only need this if multiple steps/entities are registered for this plugin)
if (target.LogicalName.Equals("msdyn_agreementbookingdate"))
{
Guid x = ((EntityReference)target.Attributes["msdyn_bookingsetup"]).Id;
// get a reference to the entity
EntityReference relatedEntityRef = new EntityReference("msdyn_agreementbookingsetup", x);
// customer can be either an account or a contact. check for account
if (relatedEntityRef.LogicalName.Equals("msdyn_agreementbookingsetup"))
{
Guid relatedEntityId = relatedEntityRef.Id; // get the guid of the related entity
// get the attribute of the related entity
Entity relatedEntity = service.Retrieve("msdyn_agreementbookingsetup", relatedEntityId, new ColumnSet("new_bookingrecurring")); // query crm for the related entity based on it's id
if (relatedEntity != null)
{
att = relatedEntity.GetAttributeValue<OptionSetValue>("new_bookingrecurring");
}
}
}
return att;
}
}
}
In Complete Source Code
Regrades