После нескольких лет, занимаясь другими делами, я вернулся к бизнесу CRM.Я уже ставлю под сомнение мой жизненный выбор.Я не понимаю, что здесь не так.Я пытаюсь создать простой плагин, который будет работать при создании Incident / Case.Он будет проверять, содержит ли поле описания действительный URL-адрес, и если да, то следует обновить первый URL-адрес, найденный в другом поле.Вот метод выполнения плагина.
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (pluginExecutionContext.InputParameters.Contains("Target") && pluginExecutionContext.InputParameters["Target"] is Entity)
{
Entity targetEntity = (Entity)pluginExecutionContext.InputParameters["Target"];
if (targetEntity.LogicalName != Incident.EntityLogicalName)
{
return;
}
IOrganizationServiceFactory orgServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService orgService = orgServiceFactory.CreateOrganizationService(pluginExecutionContext.UserId);
try
{
string desc = (string)targetEntity.Attributes["description"];
string pattern = @"\b(?:https?://|www\.)[^ \f\n\r\t\v\]]+\b";
MatchCollection collection = Regex.Matches(desc, pattern);
if (collection.Count > 0)
{
throw new Exception(collection[0].Value);
}
}
catch (Exception ex)
{
tracingService.Trace("Error in CaseUrlPlugin {0}", ex.ToString());
throw ex;
}
}
}
Проблема в том, что когда я создаю новый случай (поле описания, заполненное текстом и URL-адресом) и нажимаю кнопку Сохранить, я получаю "Данный ключ отсутствует в словареmsgstr "исключение, как будто поля описания там нет.Когда я нажимаю «ОК» в этом окне ошибок и снова нажимаю «Сохранить», тогда создается поле описания, и мой код выдает исключение с этой ссылкой.
Так почему же поле описания отсутствует в первый раз?Мне не нравится идея сделать эту операцию post, потому что это потребовало бы другой транзакции sql (чтобы сохранить инцидент снова)?