我几天前就开始了MS CRM开发,今天我花了几个小时来完成这个非常简单的基本操作,并没有找到我做错的运气。
我想在创建记录时更新联系人实体的中间名。我可以使用以下代码。
但是现在我想在打开联系人记录时做同样的事情。我确实在联系实体下注册了一个关于检索消息的新步骤。但它不起作用......没有例外。
public class IzzyPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity currentEntity = (Entity)context.InputParameters["Target"];
if (currentEntity.Attributes.Contains("middlename"))
{
currentEntity.Attributes["middlename"] = "Middle name changed";
}
else
{
currentEntity.Attributes.Add("middlename", "Middle name changed");
}
service.Update(currentEntity);
}
}
catch (Exception f)
{
throw new InvalidPluginExecutionException(f.ToString());
}
}
}
答案 0 :(得分:2)
打开记录时,您是否可以在页面加载时使用javascript设置midname值?会更容易IMO
像这样的onload会起作用
Xrm.Page.getAttribute('middlename')。setValue('New Middle Name');
如果您需要从同一实体的其他字段计算中间名,您可能需要参考XRM Page documentation on MSDN。
答案 1 :(得分:0)
我建议使用JavaScript执行此操作。我认为您不能注册打开记录时可以触发的插件。
答案 2 :(得分:0)
我不会质疑你为什么要这样做,或者你将获得中间名的值,但是我怀疑问题是关于你的插件/更新的排序。为Retrieve
消息编写插件并不是一个好主意,因为它们经常被调用。我个人认为JScript是要走的路,但是......
如果要在middlename
中插入一个值,只需将其返回给Retrieve上的用户,但没有将其提交给数据库(我知道这不是您的确切问题)然后:
> Entity currentEntity = (Entity)context.OutputParameters["Entity"];
> if(currentEntity.Attributes.contains("middlename"){
> currentEntity["middlename"] = "New value";
> }else{
> currentEntity.Attributes.Add("middlename","New value");
> }
如果要在middlename
中插入一个值并将其返回给检索上的用户并将其提交给数据库,那么我怀疑您需要将两者合并到一个插件中在Retrieve的操作前后进行注册,然后执行类似的操作(但我非常警惕甚至在每次Retreive上尝试更新......):
public class IzzyPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
var middleNameValue = "Middle name changed";
Entity currentEntity;
// Pre-stage plugin
if(context.Stage < 30){
currentEntity = (Entity)context.InputParameters["Target"];
if (currentEntity.Attributes.Contains("middlename"))
{
currentEntity.Attributes["middlename"] = middleNameValue;
}
else
{
currentEntity.Attributes.Add("middlename", middleNameValue);
}
service.Update(currentEntity);
}else{
currentEntity = (Entity)context.OutputParameters["Entity"];
if(currentEntity.Attributes.contains("middlename"){
currentEntity["middlename"] = middleNameValue;
}else{
currentEntity.Attributes.Add("middlename",middleNameValue);
}
}
}
}
catch (Exception f)
{
throw new InvalidPluginExecutionException(f.ToString());
}
}
}