[英]Dynamics 365 - Plugin Does Not Update Values
從昨天開始,我一直在追逐這一切,沒有任何意義。 我經歷了代碼的各種排列-將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;
}
}
}
任何和所有輸入表示贊賞。
我的回答基於對您的代碼的以下假設:
建議的修復程序需要進行以下更改:
出現問題的原因可能是您正在查詢工作訂單記錄以從操作前插件中獲取協議值。 工作訂單尚未保存在數據庫中。
更新的代碼:
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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.