Hi all,
I want to delete my custom entity record, when I delete appointment.
I wrote following logic:
//</snippetToDeletesi.e Initial, Full and Normal Plugin>
using System;
//using System.ServiceModel;
// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query; //FetchExpression
namespace aw_DeleteIA_FAandNSonDeletAppoint
{
public class DeleteIA_FA_NS : IPlugin
{
public void logg(IOrganizationService service, string str)
{
Entity account = new Entity("test");
account["aw_name"] = str;
// Create an account record named Fourth Coffee.
service.Create(account);
}
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
// Create the task in Microsoft Dynamics CRM.
int i = 0;
logg(service, i.ToString());
tracingService.Trace(entity.LogicalName);
if (entity.LogicalName == "appointment")
{
i++;
logg(service, i.ToString());
// An accountnumber attribute should not already exist because
// it is system generated.
if (entity.Attributes.Contains("aw_clientname") &&
entity.Attributes.Contains("aw_sessiontype") &&
entity.Attributes.Contains("aw_sessionnumber"))
{
i++;
logg(service, i.ToString());
int log = 0;
tracingService.Trace(log.ToString());
int sessionType = ((OptionSetValue)entity["aw_sessiontype"]).Value;
tracingService.Trace(sessionType.ToString());
EntityReference entityRef = (EntityReference)entity.Attributes["aw_clientname"];
tracingService.Trace(entityRef.ToString());
int sessionNumber = (int)entity.Attributes["aw_sessionnumber"];
tracingService.Trace(sessionNumber.ToString());
if (sessionType == 100000000)
{//Initial
log++;
tracingService.Trace(log.ToString());
string InitialFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
" <entity name='aw_initial'>" +
" <attribute name='aw_initialid' />" +
" <attribute name='aw_name' />" +
" <attribute name='createdon' />" +
" <order attribute='aw_name' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='aw_initial' operator='eq' value='" + entityRef.Id + "' />" +
" <condition attribute='aw_sessionnumber' operator='eq' value='" + sessionNumber + "' />" +
" </filter>" +
" </entity>" +
"</fetch>";
EntityCollection InitialQuery = service.RetrieveMultiple(new FetchExpression(InitialFetchXML));
log++;
tracingService.Trace(log.ToString());
int TotlNumberOfInitials = InitialQuery.Entities.Count;
tracingService.Trace(TotlNumberOfInitials.ToString());
if (TotlNumberOfInitials == 1)
{
tracingService.Trace(InitialQuery.Entities[0].Attributes["aw_initialid"].ToString());
service.Delete("aw_initial", (Guid)InitialQuery.Entities[0].Attributes["aw_initialid"]);
}
}
else if (sessionType == 100000001)
{//Full
log++;
tracingService.Trace(log.ToString());
string FullFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
" <entity name='aw_full'>" +
" <attribute name='aw_fullid' />" +
" <attribute name='aw_name' />" +
" <attribute name='createdon' />" +
" <order attribute='aw_name' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='aw_sessionnumber' operator='eq' value='" + sessionNumber + "' />" +
" <condition attribute='aw_full' operator='eq' uiname='Xeni' uitype='account' value='" + entityRef.Id + "' />" +
" </filter>" +
" </entity>" +
"</fetch>";
EntityCollection FullQuery = service.RetrieveMultiple(new FetchExpression(FullFetchXML));
log++;
tracingService.Trace(log.ToString());
int TotlNumberOfFull = FullQuery.Entities.Count;
tracingService.Trace(TotlNumberOfFull.ToString());
if (TotlNumberOfFull == 1)
{
tracingService.Trace(FullQuery.Entities[0].Attributes["aw_fullid"].ToString());
service.Delete("aw_full", (Guid)FullQuery.Entities[0].Attributes["aw_fullid"]);
}
}
else if (sessionType == 100000002)
{//Normal
log++;
tracingService.Trace(log.ToString());
string NormalFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
" <entity name='aw_speech'>" +
" <attribute name='aw_speechid' />" +
" <attribute name='aw_name' />" +
" <attribute name='createdon' />" +
" <order attribute='aw_name' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='aw_normal' operator='eq' uiname='Abdul' uitype='account' value='" + entityRef.Id + "' />" +
" <condition attribute='aw_sessionnumber' operator='eq' value='" + sessionNumber + "' />" +
" </filter>" +
" </entity>" +
"</fetch>";
EntityCollection NormalQuery = service.RetrieveMultiple(new FetchExpression(NormalFetchXML));
log++;
tracingService.Trace(log.ToString());
int TotlNumberOfNormal = NormalQuery.Entities.Count;
tracingService.Trace(TotlNumberOfNormal.ToString());
if (TotlNumberOfNormal == 1)
{
tracingService.Trace(NormalQuery.Entities[0].Attributes["aw_speechid"].ToString());
service.Delete("aw_speech", (Guid)NormalQuery.Entities[0].Attributes["aw_speechid"]);
}
}
}
}
}
}
}
}
//</snippetToDeletesi.e Initial, Full and Normal>
I register my plugin as shown below:
When I delete my appointment it do nothing. I checked system jobs but I did'nt see plugin execution anywhere. Where I am wrong? Please do let me know.
Thank you
Regards,
Abdul
*This post is locked for comments
Hi MёLvìN Fong
My simple question is Why I am not getting an error? If I make mistake. I am in a doubt whether my plugin is working or not on the deletion of appointment.
Thank you
Regards,
Abdul
Replace Nithya code in red:
It has to be something like this
service.Delete("aw_screening", ((EntityReference)ScreeningQuery.Entities[0].Attributes["aw_screeningid"]).Id);
instead of
service.Delete("aw_screening", (Guid)ScreeningQuery.Entities[0].Attributes["aw_screeningid"]);
//</snippetToDeleteTherapySessionsi.e Screenong, Assessment and TherapyPlugin>
using System;
//using System.ServiceModel;
// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query; //FetchExpression
using Microsoft.Xrm.Sdk.Messages;//RetrieveRequest, RetrieveResponse
//using Microsoft.Xrm.Sdk.Query;//ColumnSet
using System.ServiceModel;//FaultException<OrganizationServiceFault>
namespace aw_DeleteIA_FAandNSonDeletAppoint
{
public class DeleteIA_FA_NS : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Guid AppointmentId = context.PrimaryEntityId;
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
// Create the task in Microsoft Dynamics CRM.
if (entity.LogicalName != "appointment")
return;
try
{
RetrieveRequest ParentRequest = new RetrieveRequest();
ParentRequest.ColumnSet = new ColumnSet(new string[] { "aw_clientname", "aw_sessiontype", "aw_sessionnumber" });
ParentRequest.Target = new EntityReference("appointment", AppointmentId);
Entity RetrieveParent = (Entity)((RetrieveResponse)service.Execute(ParentRequest)).Entity;
if (entity.Attributes.Contains("aw_clientname") &&
entity.Attributes.Contains("aw_sessiontype") &&
entity.Attributes.Contains("aw_sessionnumber"))
{
int sessionType = ((OptionSetValue)entity["aw_sessiontype"]).Value;
EntityReference entityRef = (EntityReference)entity.Attributes["aw_clientname"];
int sessionNumber = (int)entity.Attributes["aw_sessionnumber"];
if (sessionType == 100000000)
{// Screening
string ScreeningFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
" <entity name='aw_screening'>" +
" <attribute name='aw_screeningid' />" +
" <attribute name='aw_name' />" +
" <attribute name='createdon' />" +
" <order attribute='aw_name' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='aw_screening' operator='eq' value='" + entityRef.Id + "' />" +
" <condition attribute='aw_sessionnumber' operator='eq' value='" + sessionNumber + "' />" +
" </filter>" +
" </entity>" +
"</fetch>";
EntityCollection ScreeningQuery = service.RetrieveMultiple(new FetchExpression(ScreeningFetchXML));
int TotlNumberOfScreenings = ScreeningQuery.Entities.Count;
if (TotlNumberOfScreenings == 1)
{
service.Delete("aw_screening", (Guid)ScreeningQuery.Entities[0].Attributes["aw_screeningid"]);
}
}
else if (sessionType == 100000001)
{// Assessment
string AssessmentFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
" <entity name='aw_screening'>" +
" <attribute name='aw_screeningid' />" +
" <attribute name='aw_name' />" +
" <attribute name='createdon' />" +
" <order attribute='aw_name' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='aw_sessionnumber' operator='eq' value='" + sessionNumber + "' />" +
" <condition attribute='aw_assessment' operator='eq' uiname='Xeni' uitype='account' value='" + entityRef.Id + "' />" +
" </filter>" +
" </entity>" +
"</fetch>";
EntityCollection AssessmentQuery = service.RetrieveMultiple(new FetchExpression(AssessmentFetchXML));
int TotlNumberOfAssessment = AssessmentQuery.Entities.Count;
tracingService.Trace(TotlNumberOfAssessment.ToString());
if (TotlNumberOfAssessment == 1)
{
service.Delete("aw_screening", (Guid)AssessmentQuery.Entities[0].Attributes["aw_screeningid"]);
}
}
else if (sessionType == 100000002)
{// Therapy Session
string TherapySessionFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
" <entity name='aw_therapysession'>" +
" <attribute name='aw_therapysessionid' />" +
" <attribute name='aw_name' />" +
" <attribute name='createdon' />" +
" <order attribute='aw_name' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='aw_therapysession' operator='eq' uiname='Abdul' uitype='account' value='" + entityRef.Id + "' />" +
" <condition attribute='aw_sessionnumber' operator='eq' value='" + sessionNumber + "' />" +
" </filter>" +
" </entity>" +
"</fetch>";
EntityCollection TherapySessionQuery = service.RetrieveMultiple(new FetchExpression(TherapySessionFetchXML));
int TotlNumberOfTherapySession = TherapySessionQuery.Entities.Count;
if (TotlNumberOfTherapySession == 1)
{
service.Delete("aw_therapysession", (Guid)TherapySessionQuery.Entities[0].Attributes["aw_therapysessionid"]);
}
}
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occured in MyPlug-in.", ex);
}
catch (Exception ex)
{
tracingService.Trace("MyPlugin: {0}", ex.ToString());
}
}
}
}
}
//</snippetToDeleteTherapySessionsi.e Screenong, Assessment and TherapyPlugin>
Hi Nithya,
The big pain point is my plugin is not giving me any kind of error. I am not able to trace it.
Thank you
Regards,
Abdul
Hi Abdul,
Even this code has the error.
You have not replaced the lines of code as I explained in my thread above.
Hi Alex, Guido and Nithya
I rewrite my code
//</snippetToDeleteTherapySessionsi.e Screenong, Assessment and TherapyPlugin>
using System;
//using System.ServiceModel;
// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query; //FetchExpression
using Microsoft.Xrm.Sdk.Messages;//RetrieveRequest, RetrieveResponse
//using Microsoft.Xrm.Sdk.Query;//ColumnSet
using System.ServiceModel;//FaultException<OrganizationServiceFault>
namespace aw_DeleteIA_FAandNSonDeletAppoint
{
public class DeleteIA_FA_NS : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Guid AppointmentId = context.PrimaryEntityId;
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
// Create the task in Microsoft Dynamics CRM.
if (entity.LogicalName != "appointment")
return;
try
{
RetrieveRequest ParentRequest = new RetrieveRequest();
ParentRequest.ColumnSet = new ColumnSet(new string[] { "aw_clientname", "aw_sessiontype", "aw_sessionnumber" });
ParentRequest.Target = new EntityReference("appointment", AppointmentId);
Entity RetrieveParent = (Entity)((RetrieveResponse)service.Execute(ParentRequest)).Entity;
if (entity.Attributes.Contains("aw_clientname") &&
entity.Attributes.Contains("aw_sessiontype") &&
entity.Attributes.Contains("aw_sessionnumber"))
{
int sessionType = ((OptionSetValue)entity["aw_sessiontype"]).Value;
EntityReference entityRef = (EntityReference)entity.Attributes["aw_clientname"];
int sessionNumber = (int)entity.Attributes["aw_sessionnumber"];
if (sessionType == 100000000)
{// Screening
string ScreeningFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
" <entity name='aw_screening'>" +
" <attribute name='aw_screeningid' />" +
" <attribute name='aw_name' />" +
" <attribute name='createdon' />" +
" <order attribute='aw_name' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='aw_screening' operator='eq' value='" + entityRef.Id + "' />" +
" <condition attribute='aw_sessionnumber' operator='eq' value='" + sessionNumber + "' />" +
" </filter>" +
" </entity>" +
"</fetch>";
EntityCollection ScreeningQuery = service.RetrieveMultiple(new FetchExpression(ScreeningFetchXML));
int TotlNumberOfScreenings = ScreeningQuery.Entities.Count;
if (TotlNumberOfScreenings == 1)
{
service.Delete("aw_screening", (Guid)ScreeningQuery.Entities[0].Attributes["aw_screeningid"]);
}
}
else if (sessionType == 100000001)
{// Assessment
string AssessmentFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
" <entity name='aw_screening'>" +
" <attribute name='aw_screeningid' />" +
" <attribute name='aw_name' />" +
" <attribute name='createdon' />" +
" <order attribute='aw_name' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='aw_sessionnumber' operator='eq' value='" + sessionNumber + "' />" +
" <condition attribute='aw_assessment' operator='eq' uiname='Xeni' uitype='account' value='" + entityRef.Id + "' />" +
" </filter>" +
" </entity>" +
"</fetch>";
EntityCollection AssessmentQuery = service.RetrieveMultiple(new FetchExpression(AssessmentFetchXML));
int TotlNumberOfAssessment = AssessmentQuery.Entities.Count;
tracingService.Trace(TotlNumberOfAssessment.ToString());
if (TotlNumberOfAssessment == 1)
{
service.Delete("aw_screening", (Guid)AssessmentQuery.Entities[0].Attributes["aw_screeningid"]);
}
}
else if (sessionType == 100000002)
{// Therapy Session
string TherapySessionFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
" <entity name='aw_therapysession'>" +
" <attribute name='aw_therapysessionid' />" +
" <attribute name='aw_name' />" +
" <attribute name='createdon' />" +
" <order attribute='aw_name' descending='false' />" +
" <filter type='and'>" +
" <condition attribute='aw_therapysession' operator='eq' uiname='Abdul' uitype='account' value='" + entityRef.Id + "' />" +
" <condition attribute='aw_sessionnumber' operator='eq' value='" + sessionNumber + "' />" +
" </filter>" +
" </entity>" +
"</fetch>";
EntityCollection TherapySessionQuery = service.RetrieveMultiple(new FetchExpression(TherapySessionFetchXML));
int TotlNumberOfTherapySession = TherapySessionQuery.Entities.Count;
if (TotlNumberOfTherapySession == 1)
{
service.Delete("aw_therapysession", (Guid)TherapySessionQuery.Entities[0].Attributes["aw_therapysessionid"]);
}
}
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occured in MyPlug-in.", ex);
}
catch (Exception ex)
{
tracingService.Trace("MyPlugin: {0}", ex.ToString());
}
}
}
}
}
//</snippetToDeleteTherapySessionsi.e Screenong, Assessment and TherapyPlugin>
Is it OK? or It still have errors
Thank you
Regards,
Abdul
Hi Abdul Wahab,
OrganizationService.Delete Method (String, Guid) accepts the entity name and GUID of the record as parameters.
In the code given, you are just passing the entity reference to the OrganizationService.Delete Method.
Please replace the below lines of code
service.Delete("aw_initial", (Guid)InitialQuery.Entities[0].Attributes["aw_initialid"]); service.Delete("aw_full", (Guid)FullQuery.Entities[0].Attributes["aw_fullid"]); service.Delete("aw_speech", (Guid)NormalQuery.Entities[0].Attributes["aw_speechid"]);
as
service.Delete("aw_initial", ((EntityReference)InitialQuery.Entities[0].Attributes["aw_initialid"]).Id); service.Delete("aw_full", ((EntityReference)FullQuery.Entities[0].Attributes["aw_fullid"]).Id); service.Delete("aw_speech", ((EntityReference)NormalQuery.Entities[0].Attributes["aw_speechid"]).Id);
Hope this helps.
Hi Alex Shlega
I want to rewrite the whole logic. Could you please help by defining something more.?
Thank you
Regards,
Abdul
This line won't work:
context.InputParameters["Target"] is Entity
It is an EntityReference, as Guido mentioned
If you don't want to rewrite the whole piece, just add a pre image to your step and use a preimage instead:
context.PreEntityImages["..."]
community.dynamics.com/.../pre-image-38-post-image-explained-33
That said, you may want to keep in mind what's mentioned here:
Hi Guido
A am not getting what you are saying. Please explain it little bit more. Sorry for the inconvenience.
Thank you
Regards, Abdul
is doing nothing because on the Delete message the Target is not an Entity but is an EntityReference
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,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156