|
![]() |
#1 |
Участник
|
Вы не поняли.
В первом пунке описано почему ошибка: потому что у Вас зациклен плагин. А во втором просто замечание по коду. Теперь, чтобы не было повторного вызова кода плагина можно добавить в начале плагина проверку X++: if(!TargetEntity.Contains("bf_survey_tkcnt")) { QueryExpression token = new QueryExpression() { EntityName = "bf_surveytoken", ColumnSet = new ColumnSet(true) }; token.Criteria.AddCondition("bf_surveytoken_survey", ConditionOperator.Equal, TargetKey.Id); List<Entity> surveyTokens = crmService.RetrieveMultiple(token).Entities.ToList(); if (surveyTokens.Count > 0) { Entity survey = new Entity(TargetEntity.LogicalName); survey.Id = TargetEntity.Id; survey["bf_survey_tkcnt"] = surveyTokens.Count.ToString(); crmService.Update(survey); } } |
|
![]() |
#2 |
Участник
|
Спасибо, помогло, разобрался. Теперь другая трабла, хочу чтобы при удалении токена, обновлялось поле в опросе (тоесть если удалю то на 1 меньше), но вот кидает ошибку (лог прикрепляю). Код вот:
Код: if (executionContext.MessageName == MessageName.Delete) { QueryExpression surveyz = new QueryExpression() { EntityName = "bf_survey", ColumnSet = new ColumnSet(true) }; surveyz.Criteria.AddCondition("bf_surveyid", ConditionOperator.Equal, TargetEntity.GetAttributeValue<EntityReference>("bf_surveytoken_survey").Id); List<Entity> surveylists = crmService.RetrieveMultiple(surveyz).Entities.ToList(); foreach (var surveylist in surveylists) { QueryExpression token = new QueryExpression() { EntityName = "bf_surveytoken", ColumnSet = new ColumnSet(true) }; token.Criteria.AddCondition("bf_surveytoken_survey", ConditionOperator.Equal, surveylist.Id); List<Entity> surveyTokens = crmService.RetrieveMultiple(token).Entities.ToList(); surveylist["bf_survey_tkcnt"] = surveyTokens.Count - 1; crmService.Update(surveylist); } } Последний раз редактировалось Lavdislav; 23.04.2014 в 17:24. Причина: Прикрепил ошибку |
|
![]() |
#3 |
Чайный пьяница
|
А может ещё ошибку сюда прикрепите?
Но моя гипотеза в следующем - при удалении записи в Target приходит не Entity а EntityReference. Для получения значения поля из лукапа рекомендую использовать Pre-Image удаляемой записи.
__________________
Эмо разработчик, сначала пишу код, потом плачу над его несовершенством. Подписывайтесь на мой блог, twitter и YouTube канал. Пользуйтесь моим Ultimate Workflow Toolkit |
|
![]() |
#4 |
Участник
|
|
|
![]() |
#5 |
Чайный пьяница
|
Скорее всего я прав. Полный код плагина предоставьте, пожалуйста.
__________________
Эмо разработчик, сначала пишу код, потом плачу над его несовершенством. Подписывайтесь на мой блог, twitter и YouTube канал. Пользуйтесь моим Ultimate Workflow Toolkit |
|
![]() |
#6 |
Участник
|
X++: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xrm.Sdk; using Bum.Survey.CRM.Plugin.BaseLib; using Microsoft.Xrm.Sdk.Query; using Microsoft.Xrm.Sdk.Messages; namespace Bum.Survey.CRM.Plugin { public class SurveyTokenAIU : IPlugin { public void Execute(IServiceProvider serviceProvider) { var process = new SurveyTokenAIUProcess(serviceProvider); process.LoadEntity(); if (!process.ValidateEntityName("bf_surveytoken")) return; if (!process.ValidateMessage(MessageName.Create, MessageName.Update, MessageName.Delete)) return; process.Run(); } } class SurveyTokenAIUProcess : bf_PluginProcess { public override void Execute() { if (TargetEntity.Contains("regardingobjectid")) { Entity record = new Entity(); Entity EntityName = (Entity)executionContext.InputParameters["Target"]; if (EntityName.Attributes.Contains("subject")) EntityName.Attributes["subject"] = EntityName.GetAttributeValue<EntityReference>("regardingobjectid").Name; if (executionContext.MessageName == MessageName.Update) { crmService.Update(EntityName); } if (executionContext.MessageName == MessageName.Update) { record = crmService.Retrieve(TargetEntity.LogicalName, TargetEntity.Id, new ColumnSet("subject", "regardingobjectid")); } else { record = TargetEntity; } string uniq = ""; int uniqLength = 1024; if (record.Contains("regardingobjectid")) { uniq = record.GetAttributeValue<EntityReference>("regardingobjectid").Name; RetrieveEntityRequest rerq = new RetrieveEntityRequest { LogicalName = record.GetAttributeValue<EntityReference>("regardingobjectid").LogicalName, RetrieveAsIfPublished = true }; RetrieveEntityResponse rers = (RetrieveEntityResponse)crmService.Execute(rerq); uniq = rers.EntityMetadata.DisplayName.UserLocalizedLabel.Label + " " + "[" + uniq + "]"; } if (!record.Contains("subject") || record["subject"].ToString() != uniq) { uniq = string.Format("{0}", uniq.Length > uniqLength ? (uniq.Substring(0, uniqLength - 3) + "...") : uniq); record["subject"] = uniq; if (executionContext.MessageName == MessageName.Update) { crmService.Update(record); } } } if (executionContext.MessageName == MessageName.Create) { QueryExpression surveyz = new QueryExpression() { EntityName = "bf_survey", ColumnSet = new ColumnSet(true) }; surveyz.Criteria.AddCondition("bf_surveyid", ConditionOperator.Equal, TargetEntity.GetAttributeValue<EntityReference>("bf_surveytoken_survey").Id); List<Entity> surveylists = crmService.RetrieveMultiple(surveyz).Entities.ToList(); foreach (var surveylist in surveylists) { QueryExpression token = new QueryExpression() { EntityName = "bf_surveytoken", ColumnSet = new ColumnSet(true) }; token.Criteria.AddCondition("bf_surveytoken_survey", ConditionOperator.Equal, surveylist.Id); List<Entity> surveyTokens = crmService.RetrieveMultiple(token).Entities.ToList(); QueryExpression tokenstart = new QueryExpression() { EntityName = "bf_surveytoken", ColumnSet = new ColumnSet(true) }; tokenstart.Criteria.AddCondition("bf_surveytoken_survey", ConditionOperator.Equal, surveylist.Id); tokenstart.Criteria.AddCondition("bf_surveytoken_sts", ConditionOperator.Equal, 100000001); List<Entity> surveyStartTokens = crmService.RetrieveMultiple(tokenstart).Entities.ToList(); QueryExpression tokenfinish = new QueryExpression() { EntityName = "bf_surveytoken", ColumnSet = new ColumnSet(true) }; tokenfinish.Criteria.AddCondition("bf_surveytoken_survey", ConditionOperator.Equal, surveylist.Id); tokenfinish.Criteria.AddCondition("bf_surveytoken_sts", ConditionOperator.Equal, 100000002); List<Entity> surveyFinishTokens = crmService.RetrieveMultiple(tokenfinish).Entities.ToList(); surveylist["bf_survey_tkfin"] = surveyStartTokens.Count; surveylist["bf_survey_tkstart"] = surveyStartTokens.Count; surveylist["bf_survey_tkcnt"] = surveyTokens.Count + 1; crmService.Update(surveylist); } } if (executionContext.MessageName == MessageName.Delete) { QueryExpression surveyz = new QueryExpression() { EntityName = "bf_survey", ColumnSet = new ColumnSet(true) }; surveyz.Criteria.AddCondition("bf_surveyid", ConditionOperator.Equal, TargetEntity.GetAttributeValue<EntityReference>("bf_surveytoken_survey").Id); List<Entity> surveylists = crmService.RetrieveMultiple(surveyz).Entities.ToList(); foreach (var surveylist in surveylists) { QueryExpression token = new QueryExpression() { EntityName = "bf_surveytoken", ColumnSet = new ColumnSet(true) }; token.Criteria.AddCondition("bf_surveytoken_survey", ConditionOperator.Equal, surveylist.Id); List<Entity> surveyTokens = crmService.RetrieveMultiple(token).Entities.ToList(); surveylist["bf_survey_tkcnt"] = surveyTokens.Count - 1; crmService.Update(surveylist); } } } public SurveyTokenAIUProcess(IServiceProvider serviceProvider) : base(serviceProvider) { } } } |
|
![]() |
#7 |
Чайный пьяница
|
А код bf_PluginProcess? В конструкторе ведь наверняка TargetEntity заполяете...
__________________
Эмо разработчик, сначала пишу код, потом плачу над его несовершенством. Подписывайтесь на мой блог, twitter и YouTube канал. Пользуйтесь моим Ultimate Workflow Toolkit |
|
|
Опции темы | Поиск в этой теме |
Опции просмотра | |
|