经过几年做其他事情后,我又回到了CRM业务。我已经在质疑我的生活选择。我不明白这里有什么问题。我正在尝试创建一个简单的插件,它将在事件/案例创建时运行。它将查看描述字段是否包含有效的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)并点击保存时,我得到“给定键不存在于字典中”例外,好像描述字段不在那里。当我点击确定该错误窗口并再次点击保存时,会建立描述字段,我的代码会抛出该链接的异常。
那么为什么第一次出现描述字段呢?我不喜欢这个post post操作的想法因为那需要另一个sql事务权限(再次保存事件)?
Aah ffs。问题是,当我第一次尝试保存时,我使用了CTRL + S而我的焦点仍然在描述字段,因为这是我填写的最后一个字段。现在看来UI没有注册要填充的字段,如果专注于该字段并点击CTRL + S.点击保存图标有效,因为这将使焦点脱离描述字段。当然,在我的情况下,该错误窗口的确定也没有聚焦场,因此第二次保存工作.............
好吧,至少我在这里发布问题之后就知道了。一直试图解决这个问题太久了。