如何使用CRM Dynamics 2013/2015中的插件更新CRM中的实体与相关实体
本文关键字:实体 CRM 何使用 更新 插件 2015 Dynamics 2013 | 更新日期: 2023-09-27 18:13:09
我是CRM
的新手,我正在努力使用插件更新实体,我已经在PhoneCall
实体上注册了一个插件,用于创建消息的预操作,然后当我查询有关实体并将有关实体的数据写入我的PhoneCall
实体时,尽管我似乎得到了一个错误,即GUID
xxxxx的记录不存在。
我的代码如下;
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context == null)
{
throw new ArgumentNullException("localContext");
}
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity phoneCallEntity = (Entity)context.InputParameters["Target"];
if (phoneCallEntity.LogicalName != "phonecall")
return;
if (context.MessageName == "Create")
{
try
{
QueryExpression qe = new QueryExpression("new_zoiperhistorydata");
qe.ColumnSet = new ColumnSet("new_regardingobjectid", "new_callduration");
var ZoiperData = service.RetrieveMultiple(qe);
if (ZoiperData != null && ZoiperData.Entities.Count > 0)
{
foreach (Entity entity in ZoiperData.Entities)
{
if (entity.Attributes.Contains("new_callduration"))
phoneCallEntity.Attributes["new_callduration"] = entity.Attributes["new_callduration"].ToString();
}
}
service.Update(phoneCallEntity);
}
catch (System.Exception ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine(ex.Message);
// Display the details of the inner exception.
if (ex.InnerException != null)
{
Console.WriteLine(ex.InnerException.Message);
FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>;
if (fe != null)
{
Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
Console.WriteLine("Code: {0}", fe.Detail.ErrorCode);
Console.WriteLine("Message: {0}", fe.Detail.Message);
Console.WriteLine("Trace: {0}", fe.Detail.TraceText);
Console.WriteLine("Inner Fault: {0}",
null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
}
}
}
}
service.Update(phoneCallEntity)
上的代码失败;
问题是您在Create事件的pre-operation阶段运行插件。实际上,在此阶段没有创建电话(并且没有Id),您将得到报告的错误消息。实际上,您不必使用服务自己更新电话。更新方法。由于操作运行,您可以只更新目标上喜欢的字段,让CRM完成其余的工作:)