Показать сообщение отдельно
Старый 24.07.2012, 12:26   #6  
Taker1796 is offline
Taker1796
Участник
Аватар для Taker1796
 
112 / 11 (1) +
Регистрация: 22.04.2012
Цитата:
Сообщение от a33ik Посмотреть сообщение
Там действительно EntityReference. Если не работает - ошибка у вас в коде. Полный код приведите, пожалуйста.
Вот код, который работает на update/create. Я понял, что событие Delete работает только с EntityReference. Но дело в том, что у меня в коде запрос, который я, к сожалению, не знаю как переписать при событии Delete c EntityReference. Смысл работы кода в том, что плагин подсчитывает количество пользователей и проставляет значение в account. Пользователи и account связаны 1:N
Код:
 public void Execute(IServiceProvider serviceProvider)
        {
            int Count = 0;
            Entity entity = null;
            // Получаем контекст плагина
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                // Получаем объект
                entity = (Entity)context.InputParameters["Target"];
            }
            else
            {
                return;
            }
            if (entity.Attributes.Contains("parentcustomerid"))
            {
                // Получаем сервис через контекст плагина
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                ConditionExpression condition = new ConditionExpression();
                condition.AttributeName = "parentcustomerid";
                condition.Operator = ConditionOperator.Equal;
                condition.Values.Add(entity.GetAttributeValue<EntityReference>("parentcustomerid").Id.ToString());

                FilterExpression filter = new FilterExpression();
                filter.Conditions.Add(condition);

                QueryExpression query = new QueryExpression("contact");
                query.ColumnSet.AddColumns(new string[] { "fullname" });
                query.Criteria.AddFilter(filter);

                EntityCollection result = service.RetrieveMultiple(query);
                foreach (var a in result.Entities)
                {
                    Count++;
                }
                Entity account = new Entity("account");
                ColumnSet attributes = new ColumnSet(new string[] { "new_contactquantity" });

                account = service.Retrieve(account.LogicalName, entity.GetAttributeValue<EntityReference>("parentcustomerid").Id, attributes);
                //Обновляем значение поля "Количество клиентов"
                account["new_contactquantity"] = Count;

                // Обновляем запись
                service.Update(account);
            }

Последний раз редактировалось Taker1796; 24.07.2012 в 12:28.