从昨天开始,我一直在追逐这件事,没有任何意义。我经历了代码的各种排列-将Decimal类型函数更改为String并返回String而不是十进制,使用了十进制数字,在if语句中硬编码值(不使用变量),和使用变量本身,但似乎没有任何效果。如果我仅在try块的开头设置字段值,并且不执行任何类型的逻辑,则说明字段已过时。当我通过插件注册工具中的Plugin Profiles / Debug逐步执行代码时,我看到设置值的代码行被命中,没有抛出异常,但是再次,值没有被更新。我什至试图添加service.Update(entity);但这也不起作用(当我在没有任何逻辑的情况下设置字段的值时,就不需要service.Update调用来保存值)。
我要在CRM中写入的字段是2精度的十进制类型。当然,C#中的十进制类型具有8或10的精度,因此我什至尝试修剪小数,将小数转换为字符串等,但无济于事。
有什么想法可以帮助我弄清楚这里发生了什么吗?
我在此处发布了代码的最简单版本-在我将Decimal类型函数更改为string等之前,就已发布
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;
//}
}
}
现在,在这里您可以看到我当前的代码,在这里我正在做很多疯狂的事情-首先,使用String类型的函数取回十进制值。其次,调用一个函数以实际设置值,只是为了证明现有函数没有什么问题(并允许更轻松地格式化该行)。不管我做什么,我都无法使这当之无愧的工作!
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;
}
}
}
任何和所有输入表示赞赏。
答案 0 :(得分:0)
我的回复基于以下有关您的代码的假设:
建议的修复程序需要进行以下更改:
问题的原因可能是您正在查询工作订单记录以从操作前插件获取协议值。工作订单尚未保存在数据库中。
更新代码:
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 &&
context.PreEntityImages != null && context.PreEntityImages.Contains("PreImage"))
{
Entity target = (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 = target.GetAttributeValue<Guid>("msdyn_workorderid");
EntityReference RefEntityID = null;
if (target.Attributes.Contains("msdyn_agreement"))
{
RefEntityID = target.GetAttributeValue<EntityReference>("msdyn_agreement");
}
//if it has a value, continue
if (RefEntityID != null)
{
Decimal RefEntityFieldValue = GetDecFieldValueFrmID(service, "msdyn_agreement", "msdyn_agreementid", RefEntityID.Id, "client_ntepercent");
//if it has a value, continue
if (RefEntityFieldValue > -99999999)
{
target["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 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;
}
}