Переписала. Не помогло. Скидываю полный текст плагина, может быть, кто подскажет, что я сделала не так.
X++:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Win32;
using Softline.MsCrm40.Sdk;
using Softline.MsCrm40;
using Softline.Runov.Apps.Plugins_new.DataAccess;
namespace Softline.Runov.Apps.Plugins_new
{
public class PostCreateGroupMember : Plugin
{
public PostCreateGroupMember(string unsecureInfo, string secureInfo)
: base(unsecureInfo, secureInfo)
{
}
public override void Execute(IPluginExecutionContext context)
{
try
{
if (PluginConfiguration.LogIncomingMessages)
{
Logger.WriteInfo("PostCreateGroupMember Plug-in running.\nPluginExecutionContext:\n" + PluginHelper.GetContextXml(context));
}
if (context.CallerOrigin is OfflineOrigin)
return;
DynamicEntity TargetEntityLocal = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];
if (context.MessageName == "Create" && TargetEntityLocal.Properties.Contains("new_groupid"))
{
CreateVisits(context);
}
UpdateGroupData(context);
}
catch (Exception ex)
{
StringBuilder errorMessage = new StringBuilder();
errorMessage.AppendFormat("Plug-in {0} failed\n", this.GetType().ToString());
errorMessage.AppendFormat("PrimaryEntityName: {0}\n", context.PrimaryEntityName);
errorMessage.AppendFormat("MessageName: {0}\n", context.MessageName);
errorMessage.AppendFormat("Error: {0}\n", ex.Message);
InvalidPluginExecutionException invalidPluginExecutionException = new InvalidPluginExecutionException(errorMessage.ToString(), ex);
invalidPluginExecutionException.Data.Add("PluginExecutionContext", PluginHelper.GetContextXml(context));
Logger.WriteError(invalidPluginExecutionException);
throw invalidPluginExecutionException;
}
finally
{
Finally();
}
}
private void UpdateGroupData(IPluginExecutionContext context)
{
CrmData crmData = new CrmData(context);
GroupMember groupMember = new GroupMember(context);
crmData.UpdateGroupData(groupMember.PostGroupId);
}
/// <summary>
/// Создание посещений при добавлении нового обучающегося в группу
/// </summary>
private void CreateVisits(IPluginExecutionContext context)
{
Guid groupId = ((Lookup)GetEntityPropertyLocal(context, "new_groupid")).Value;
DynamicEntity[] lessons = GroupsDataAccess.RetrieveLessonsByGroup(CrmService, groupId, "new_name", "new_groupid");
DynamicEntity contract = CrmService.RetrieveDynamic("opportunity", ((Lookup)GetEntityPropertyLocal(context, "new_contractid")).Value, "new_numfirstles");
int firstlesson = 0;
if (contract.Properties.Contains("new_numfirstles"))
{
firstlesson = ((CrmNumber)contract["new_numfirstles"]).Value;
}
Guid GroupMemberIdLocal = (Guid)context.OutputParameters.Properties[ParameterName.Id];
foreach (DynamicEntity lesson in lessons)
{
string lessonname = lesson["new_name"].ToString();
if (int.Parse(lessonname) >= firstlesson)
{
VisitsDataAccess.CreateVisit(CrmService, lessonname, ((Key)lesson["new_lessonsid"]).Value, GroupMemberIdLocal);
}
}
}
protected object GetEntityPropertyLocal(IPluginExecutionContext context, string propertyName)
{
DynamicEntity TargetEntityLocal = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];
if (context.Stage == MessageProcessingStage.AfterMainOperationOutsideTransaction)
{
DynamicEntity _postEntityImageLocal = GetPostEntityImageLocal(context, TargetEntityLocal.Name);
if (_postEntityImageLocal == null)
{
if (TargetEntityLocal.Properties.Contains(propertyName))
{
return TargetEntityLocal[propertyName];
}
}
else if (_postEntityImageLocal.Properties.Contains(propertyName))
{
return _postEntityImageLocal[propertyName];
}
return null;
}
else // (_currentContext.Stage == MessageProcessingStage.BeforeMainOperationOutsideTransaction)
{
if (!TargetEntityLocal.Properties.Contains(propertyName))
{
DynamicEntity _preEntityImageLocal = GetPreEntityImageLocal(context, TargetEntityLocal.Name);
if (_preEntityImageLocal == null)
return null;
else if (_preEntityImageLocal.Properties.Contains(propertyName))
{
return _preEntityImageLocal[propertyName];
}
return null;
}
return TargetEntityLocal[propertyName];
}
}
protected static DynamicEntity GetPreEntityImageLocal(IPluginExecutionContext context, string _EntityName)
{
DynamicEntity preImageEntity = null;
if (context.PreEntityImages.Contains(_EntityName))
preImageEntity = context.PreEntityImages[_EntityName] as DynamicEntity;
return preImageEntity;
}
protected static DynamicEntity GetPostEntityImageLocal(IPluginExecutionContext context, string _EntityName)
{
DynamicEntity postImageEntity = null;
if (context.PostEntityImages.Contains(_EntityName))
postImageEntity = context.PostEntityImages[_EntityName] as DynamicEntity;
return postImageEntity;
}
}
}