【发布时间】:2018-09-11 18:09:15
【问题描述】:
从昨天开始我就一直在追查这个,没有任何意义。我已经对代码的外观进行了各种排列 - 将 Decimal 类型函数更改为 String 并返回 String 而不是小数,使用十进制数,在 if 语句中对值进行硬编码(不使用变量),并使用变量本身,但似乎没有任何效果。如果我只是在 try 块的开头设置字段值,并且不执行任何类型的逻辑,则这些字段正在被更新。当我通过插件注册工具中的插件配置文件/调试单步执行代码时,我可以看到设置值的代码行正在被命中,并且没有抛出异常,但同样,值没有被更新。我什至尝试添加 service.Update(entity);但这也不起作用(当我在没有任何逻辑的情况下设置字段的值时,我不需要 service.Update 调用来保存值)。
我试图在 CRM 中写入的字段是具有 2 精度的小数类型。当然,C# 中的小数类型是 8 或 10 精度的,所以我什至尝试修剪小数,将小数转换为字符串等,但没有任何效果。
有什么想法可以帮助我弄清楚这里发生了什么吗?
我已经在这里发布了我的代码的最简单版本-在我去将小数类型函数更改为字符串等之前。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.Net;
using System.Net.Mail;
namespace ClientNTE
{
public class UpdateVals : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
//Get current record's GUID, which will always be in the attributes collection
Guid MainEntityID = new Guid(entity["msdyn_workorderid"].ToString());
//Get related entity record's GUID from generic field value grabber function against the main entity
Guid RefEntityID = GetGUIDFieldValueFrmID(service, "msdyn_workorder", "msdyn_workorderid", MainEntityID, "msdyn_agreement");
//if it has a value, continue
if (RefEntityID != Guid.Empty)
{
Decimal RefEntityFieldValue = GetDecFieldValueFrmID(service, "msdyn_agreement", "msdyn_agreementid", RefEntityID, "client_ntepercent");
//if it has a value, continue
if (RefEntityFieldValue > -99999999)
{
entity["client_ntepercent"] = RefEntityFieldValue;
}
}
}
catch (Exception ex)
{
//write errors to the CRM Plugin Trace Log
tracingService.Trace("clientNTE - Agreement To Work Order - ", ex.ToString());
//Throw error through UI
throw new InvalidPluginExecutionException("Error, Please See Plugin Log");
}
}
}
public Guid GetGUIDFieldValueFrmID(IOrganizationService svc, String EntityNm, String EntityIDField, Guid EntityIDValue, String ReturnFieldNm)
{
Guid retval = Guid.Empty;
try
{
OrganizationServiceContext orgContext = new OrganizationServiceContext(svc);
var ReturnRecords = from a in orgContext.CreateQuery(EntityNm)
where (Guid)a[EntityIDField] == EntityIDValue
select new
{
FieldVal = a[ReturnFieldNm]
};
if (ReturnRecords != null)
{
foreach (var EvalRec in ReturnRecords)
{
retval = ((Microsoft.Xrm.Sdk.EntityReference)EvalRec.FieldVal).Id;
}
}
else
{
retval = Guid.Empty;
}
}
catch (Exception ex)
{
retval = Guid.Empty;
//Throw error through UI
throw new InvalidPluginExecutionException(ex.ToString());
}
return retval;
}
public Decimal GetDecFieldValueFrmID(IOrganizationService svc, String EntityNm, String EntityIDField, Guid EntityIDValue, String ReturnFieldNm)
{
Decimal retval = -99999999;
try
{
OrganizationServiceContext orgContext = new OrganizationServiceContext(svc);
var ReturnRecords = from a in orgContext.CreateQuery(EntityNm)
where (Guid)a[EntityIDField] == EntityIDValue
select new
{
FieldVal = a[ReturnFieldNm]
};
if (ReturnRecords != null)
{
foreach (var EvalRec in ReturnRecords)
{
retval = Convert.ToDecimal(EvalRec.FieldVal);
}
}
else
{
retval = -99999999;
}
}
catch (Exception ex)
{
retval = -99999999;
//Throw error through UI
throw new InvalidPluginExecutionException(ex.ToString());
}
return retval;
}
//public static Boolean UpdateParentRecord(IOrganizationService svc, Guid ParentRecordID, String FieldValue)
//{
// Boolean retval = false;
// try
// {
// Entity parent_entityrec = new Entity("parent_entity");
// parent_entityrec["fieldtoupdate"] = FieldValue;
// svc.Update(parent_entityrec);
// retval = true;
// }
// catch (Exception ex)
// {
// retval = false;
// }
// return retval;
//}
}
}
现在,在这里你可以看到我当前的代码,我正在做一堆疯狂的事情——首先,使用字符串类型的函数来取回十进制值。其次,调用一个函数来实际设置值,只是为了证明现有函数没有问题(并允许更轻松地进行格式化)。不管我做什么,我都不能让这该死的东西工作!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using System.Net;
using System.Net.Mail;
namespace CLIENTNTE
{
public class AgreementToWorkOrder : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
//string Num = 12.500000.ToString();
//entity["CLIENT_testdecimal"] = Num;
//entity["CLIENT_ntepercent"] = Num;
//Get current record's GUID, which will always be in the attributes collection
Guid MainEntityID = new Guid(entity["msdyn_workorderid"].ToString());
//Get related entity record's GUID from generic field value grabber function against the main entity
Guid RefEntityID = GetGUIDFieldValueFrmID(service, "msdyn_workorder", "msdyn_workorderid", MainEntityID, "msdyn_agreement");
//if it has a value, continue
if (RefEntityID != Guid.Empty)
{
String RefEntityFieldValue = GetDecFieldValueFrmID(service, "msdyn_agreement", "msdyn_agreementid", RefEntityID, "CLIENT_ntepercent");
decimal decVal = Convert.ToDecimal(RefEntityFieldValue);
//if it has a value, continue
if (decVal > -99999999)
{
// entity["CLIENT_ntepercent"] = RefEntityFieldValue;
// entity["CLIENT_ntepercent"] = "12.5";// RefEntityFieldValue;
//entity["CLIENT_testdecimal"] = "13.5";// RefEntityFieldValue;
setDecimal(RefEntityFieldValue, serviceProvider);
//service.Update(entity);
}
}
}
catch (Exception ex)
{
//write errors to the CRM Plugin Trace Log
tracingService.Trace("CLIENTNTE - Agreement To Work Order - ", ex.ToString());
//Throw error through UI
throw new InvalidPluginExecutionException("Error, Please See Plugin Log");
}
}
}
public void setDecimal(String RefEntityFieldValue, IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
//IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
Entity entity = (Entity)context.InputParameters["Target"];
//Guid MainEntityID = new Guid(entity["msdyn_workorderid"].ToString());
//Guid RefEntityID = GetGUIDFieldValueFrmID(service, "msdyn_workorder", "msdyn_workorderid", MainEntityID, "msdyn_agreement");
//String RefEntityFieldValue = GetDecFieldValueFrmID(service, "msdyn_agreement", "msdyn_agreementid", RefEntityID, "CLIENT_ntepercent"); */
// entity["CLIENT_testdecimal"] = RefEntityFieldValue;
entity["CLIENT_testdecimal"] = 12;
entity["CLIENT_ntepercent"] = RefEntityFieldValue;
}
public Guid GetGUIDFieldValueFrmID(IOrganizationService svc, String EntityNm, String EntityIDField, Guid EntityIDValue, String ReturnFieldNm)
{
Guid retval = Guid.Empty;
try
{
OrganizationServiceContext orgContext = new OrganizationServiceContext(svc);
var ReturnRecords = from a in orgContext.CreateQuery(EntityNm)
where (Guid)a[EntityIDField] == EntityIDValue
select new
{
FieldVal = a[ReturnFieldNm]
};
if (ReturnRecords != null)
{
foreach (var EvalRec in ReturnRecords)
{
retval = ((Microsoft.Xrm.Sdk.EntityReference)EvalRec.FieldVal).Id;
}
}
else
{
retval = Guid.Empty;
}
}
catch (Exception ex)
{
retval = Guid.Empty;
throw new InvalidPluginExecutionException(ex.ToString());
}
return retval;
}
public String GetDecFieldValueFrmID(IOrganizationService svc, String EntityNm, String EntityIDField, Guid EntityIDValue, String ReturnFieldNm)
{
Decimal retval = -99999999;
String stringVal = "";
try
{
OrganizationServiceContext orgContext = new OrganizationServiceContext(svc);
var ReturnRecords = from a in orgContext.CreateQuery(EntityNm)
where (Guid)a[EntityIDField] == EntityIDValue
select new
{
FieldVal = a[ReturnFieldNm]
};
if (ReturnRecords != null)
{
foreach (var EvalRec in ReturnRecords)
{
retval = Convert.ToDecimal(EvalRec.FieldVal);
// stringVal = retval.ToString().TrimEnd('0', '.');
stringVal = retval.ToString();
}
}
else
{
retval = -99999999;
}
}
catch (Exception ex)
{
retval = -99999999;
throw new InvalidPluginExecutionException(ex.ToString());
}
return stringVal;
}
}
}
任何和所有输入表示赞赏。
【问题讨论】:
-
您是否检查过您的插件步骤是否未注册以运行后期操作?我看不到对 IOrganizationService.Update 方法的调用,所以我认为这是预操作。
-
我应该在原始帖子中包含更多关于我的插件的上下文,我的错。它运行工单的 OnCreate,从不更新,只是从协议(父)中提取数据以填充 WO(子)。 OOB 映射不起作用,并且使用 Workflows 妨碍了 Work Order 实体的其他 onChange 插件逻辑,因此我不得不将其分解为自己的代码段。这被注册为一个预验证插件(我完全赞成改变它 - 我仍然没有完全理解 pre-val,pre-op,post-op)。
标签: c# dynamics-crm microsoft-dynamics dynamics-365