我正在使用“删除”按钮上的插件,当我删除任何选定的记录时,所有不同的(选定记录)也应该被删除。
在我的情况下,我有一位受邀参加会议的与会者,参会者也有一些讨论点记录和行动项目记录,根据我删除会议参加者的要求,它应该删除此参加者有任何讨论点记录和行动项目记录。需要这方面的帮助。
以下是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
namespace SFDSendEmail.SFDDeleteAttendee.Class
{
public class SFDDeleteAttendee : IPlugin
{
Guid Internaluser;
Guid Externaluser;
private IOrganizationService _sdk = null;
public void Execute(IServiceProvider serviceProvider)
{
try
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
_sdk = (IOrganizationService)factory.CreateOrganizationService(context.UserId);
if (context.IsExecutingOffline || context.IsOfflinePlayback)
return;
// The InputParameters collection contains all the data passed
// in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is EntityReference)
{
// EntityReference RequiredAttendee = context.InputParameters["new_requiredattendee"] as EntityReference;
// Entity entity = _sdk.Retrieve("new_requiredattendee", ((EntityReference)RequiredAttendee["new_requiredattendeeid"]).Id, new ColumnSet(true));
// Entity eUser = _sdk.Retrieve(RequiredAttendee.LogicalName, RequiredAttendee.Id, new ColumnSet(true));
EntityReference RequiredAttendee = (EntityReference)context.InputParameters["Target"];
Entity eUser = _sdk.Retrieve(RequiredAttendee.LogicalName, RequiredAttendee.Id, new ColumnSet(true));
if (context.MessageName == "Delete")
{
if (eUser.LogicalName != "new_requiredattendee")
{
return;
}
else
{
//If User Selects the Interner User for Deletion
if (eUser.Attributes.Contains("new_internaluser"))
{
//Save Internal User ID
Internaluser = ((EntityReference)eUser["new_internaluser"]).Id;
//Function to fetch the Meeting Attendee with the Above ID
Guid VerifyAttendee = VerifyMeetingAttendee(eUser,_sdk,Internaluser);
//If its the Meeting Attendee
if (VerifyAttendee != null)
{
//Get the Attendee with its Discussion Point
Guid AttendeewithDp = VerifyDPMeetingAttendee(eUser, _sdk, Internaluser);
if (AttendeewithDp != null)
{
//Get the Attendee with Action item
Guid AttendeewithAI = VerifyAIMeetingAttendee(eUser, _sdk, Internaluser);
if (AttendeewithAI != null)
{
//DO your code here....................................
_sdk.Delete("new_requiredattendee", AttendeewithAI);
}
}
}
}
//If User Selects the Interner User for Deletion
else if (eUser.Attributes.Contains("new_externaluser"))
{
//Save Internal User ID
Externaluser = ((EntityReference)eUser.Attributes["new_externaluser"]).Id;
//Function to fetch the Meeting Attendee with the Above ID
Guid verifyExternalAttendee = VerifyExternalMeetingAttendee(eUser,_sdk,Externaluser);
if (verifyExternalAttendee != null)
{
//Get the Attendee with its Discussion Point
Guid EXAttendeewithDp = VerifyEXDPMeetingAttendee(eUser, _sdk, Externaluser);
if (EXAttendeewithDp != null)
{
//Get the Attendee with Action item
Guid EXAttendeewithAI = VerifyEXAIMeetingAttendee(eUser, _sdk, Externaluser);
if (EXAttendeewithAI != null)
{
//DO your code here....................................
_sdk.Delete("new_requiredattendee", EXAttendeewithAI);
}
}
}
}
}
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
//Get Internal Meeting Attendee
public Guid VerifyMeetingAttendee(Entity RequiredAttendee,IOrganizationService _orgService,Guid user)
{
QueryExpression GetMeetingAttendees = new QueryExpression();
GetMeetingAttendees.EntityName = "new_requiredattendee";
GetMeetingAttendees.ColumnSet = new ColumnSet("new_internaluser");
GetMeetingAttendees.Criteria.AddCondition("new_mom", ConditionOperator.Equal, RequiredAttendee["new_mom"]);
GetMeetingAttendees.Criteria.AddCondition("new_internaluser", ConditionOperator.Equal,user);
GetMeetingAttendees.Criteria.AddCondition("new_actionitem", ConditionOperator.Null);
GetMeetingAttendees.Criteria.AddCondition("new_discussionpoint", ConditionOperator.Null);
GetMeetingAttendees.Criteria.AddCondition("new_externaluser", ConditionOperator.Null);
EntityCollection GMA = _orgService.RetrieveMultiple(GetMeetingAttendees);
return (Guid)GMA[0]["new_internaluser"];
}
//Get Internal Meeting Attendee with Discussion Point
public Guid VerifyDPMeetingAttendee(Entity RequiredAttendee, IOrganizationService _orgService, Guid user)
{
QueryExpression GetDPMeetingAttendees = new QueryExpression();
GetDPMeetingAttendees.EntityName = "new_requiredattendee";
GetDPMeetingAttendees.ColumnSet = new ColumnSet("new_internaluser");
GetDPMeetingAttendees.Criteria.AddCondition("new_mom", ConditionOperator.Equal, RequiredAttendee["new_mom"]);
GetDPMeetingAttendees.Criteria.AddCondition("new_internaluser", ConditionOperator.Equal,user);
GetDPMeetingAttendees.Criteria.AddCondition("new_actionitem", ConditionOperator.Null);
GetDPMeetingAttendees.Criteria.AddCondition("new_discussionpoint", ConditionOperator.NotNull);
GetDPMeetingAttendees.Criteria.AddCondition("new_externaluser", ConditionOperator.Null);
EntityCollection GMA = _orgService.RetrieveMultiple(GetDPMeetingAttendees);
return (Guid)GMA[0]["new_internaluser"];
}
//Get Internal Meeting Attendee with Action Items
public Guid VerifyAIMeetingAttendee(Entity RequiredAttendee, IOrganizationService _orgService, Guid user)
{
QueryExpression GetDPMeetingAttendees = new QueryExpression();
GetDPMeetingAttendees.EntityName = "new_requiredattendee";
GetDPMeetingAttendees.ColumnSet = new ColumnSet("new_internaluser");
GetDPMeetingAttendees.Criteria.AddCondition("new_mom", ConditionOperator.Equal, RequiredAttendee["new_mom"]);
GetDPMeetingAttendees.Criteria.AddCondition("new_internaluser", ConditionOperator.Equal,user);
GetDPMeetingAttendees.Criteria.AddCondition("new_actionitem", ConditionOperator.NotNull);
GetDPMeetingAttendees.Criteria.AddCondition("new_externaluser", ConditionOperator.Null);
EntityCollection GMA = _orgService.RetrieveMultiple(GetDPMeetingAttendees);
return (Guid)GMA[0]["new_internaluser"];
}
//Get External Meeting Attendee
public Guid VerifyExternalMeetingAttendee(Entity RequiredAttendee, IOrganizationService _orgService, Guid user)
{
QueryExpression GetMeetingAttendees = new QueryExpression();
GetMeetingAttendees.EntityName = "new_requiredattendee";
GetMeetingAttendees.ColumnSet = new ColumnSet("new_externaluser");
GetMeetingAttendees.Criteria.AddCondition("new_mom", ConditionOperator.Equal, RequiredAttendee["new_mom"]);
GetMeetingAttendees.Criteria.AddCondition("new_externaluser", ConditionOperator.Equal, user);
GetMeetingAttendees.Criteria.AddCondition("new_actionitem", ConditionOperator.Null);
GetMeetingAttendees.Criteria.AddCondition("new_discussionpoint", ConditionOperator.Null);
GetMeetingAttendees.Criteria.AddCondition("new_internaluser", ConditionOperator.Null);
EntityCollection GMA = _orgService.RetrieveMultiple(GetMeetingAttendees);
return (Guid)GMA[0]["new_externaluser"];
}
//Get External Meeting Attendee with Discussion Point
public Guid VerifyEXDPMeetingAttendee(Entity RequiredAttendee, IOrganizationService _orgService, Guid user)
{
QueryExpression GetMeetingAttendees = new QueryExpression();
GetMeetingAttendees.EntityName = "new_requiredattendee";
GetMeetingAttendees.ColumnSet = new ColumnSet("new_externaluser");
GetMeetingAttendees.Criteria.AddCondition("new_mom", ConditionOperator.Equal, RequiredAttendee["new_mom"]);
GetMeetingAttendees.Criteria.AddCondition("new_externaluser", ConditionOperator.Equal, user);
GetMeetingAttendees.Criteria.AddCondition("new_actionitem", ConditionOperator.Null);
GetMeetingAttendees.Criteria.AddCondition("new_discussionpoint", ConditionOperator.NotNull);
GetMeetingAttendees.Criteria.AddCondition("new_internaluser", ConditionOperator.Null);
EntityCollection GMA = _orgService.RetrieveMultiple(GetMeetingAttendees);
return (Guid)GMA[0]["new_externaluser"];
}
//Get External Meeting Attendee with Action Item
public Guid VerifyEXAIMeetingAttendee(Entity RequiredAttendee, IOrganizationService _orgService, Guid user)
{
QueryExpression GetMeetingAttendees = new QueryExpression();
GetMeetingAttendees.EntityName = "new_requiredattendee";
GetMeetingAttendees.ColumnSet = new ColumnSet("new_externaluser");
GetMeetingAttendees.Criteria.AddCondition("new_mom", ConditionOperator.Equal, RequiredAttendee["new_mom"]);
GetMeetingAttendees.Criteria.AddCondition("new_externaluser", ConditionOperator.Equal, user);
GetMeetingAttendees.Criteria.AddCondition("new_actionitem", ConditionOperator.NotNull);
GetMeetingAttendees.Criteria.AddCondition("new_internaluser", ConditionOperator.Null);
EntityCollection GMA = _orgService.RetrieveMultiple(GetMeetingAttendees);
return (Guid)GMA[0]["new_externaluser"];
}
}
}
答案 0 :(得分:0)
我在这里理解这个问题有点麻烦,但这是我的想法。
1)如果它正在回滚删除,那么删除时会出现错误,或者在此删除下有级联删除。提供异常消息对于帮助我们解决您的问题大有帮助。
2)您可以使用LINQ查询而不是CRM QueryExpression使您的代码更简洁和可读。另一个需要改进的地方是完全相同的代码多次,带有1个小调整 - 也就是attributes.contains(“new_internaluser”)和attributes.contains(“new_external”)用户的分支可以大大改进 - 见下文。
//Add this method to the class
internal void deleteUser(string userString){
user = ((EntityReference)eUser[userString]).Id;
//Function to fetch the Meeting Attendee with the Above ID
Guid VerifyAttendee = VerifyMeetingAttendee(eUser,_sdk,user);
//If its the Meeting Attendee
if (VerifyAttendee != null)
{
//Get the Attendee with its Discussion Point
Guid AttendeewithDp = VerifyDPMeetingAttendee(eUser, _sdk, user);
if (AttendeewithDp != null)
{
//Get the Attendee with Action item
Guid AttendeewithAI = VerifyAIMeetingAttendee(eUser, _sdk, user);
if (AttendeewithAI != null)
{
//DO your code here....................................
_sdk.Delete("new_requiredattendee", AttendeewithAI);
}
}
}
}
//And call it with these few simple lines within your main execute method:
if (eUser.Attributes.Contains("new_internaluser"))
{
deleteUser("new_internaluser");
}
//If User Selects the Interner User for Deletion
else if (eUser.Attributes.Contains("new_externaluser"))
{
deleteUser("new_externaluser");
}