AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Разработка
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 13.03.2013, 18:01   #7  
probka is offline
probka
Участник
Аватар для probka
 
98 / 11 (1) +
Регистрация: 20.02.2012
Адрес: Ростов-на-Дону
Переписала. Не помогло. Скидываю полный текст плагина, может быть, кто подскажет, что я сделала не так.

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;
        }

   
    }
}
__________________
Так победим!

 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
crminthefield: Podcast and Overview: Microsoft Dynamics CRM 2011 Update Rollup 12 Blog bot Dynamics CRM: Blogs 0 30.01.2013 01:11
crminthefield: Podcast and Overview: Microsoft Dynamics CRM 2011 Update Rollup 10 Blog bot Dynamics CRM: Blogs 0 17.08.2012 03:27
crminthefield: Podcast and Overview: Microsoft Dynamics CRM 2011 Update Rollup 7 Blog bot Dynamics CRM: Blogs 0 27.03.2012 02:11
crminthefield: Podcast and Overview: Microsoft Dynamics CRM 2011 Update Rollup 4 Blog bot Dynamics CRM: Blogs 0 24.09.2011 01:16
CRM DE LA CREME! Configuring Microsoft Dynamics CRM 4.0 for Internet-facing deployment Blog bot Dynamics CRM: Blogs 0 18.08.2009 11:05
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 03:41.