我有一个与 crm 货币字段(estimatedrevenue)有关的问题,这是自定义字段和小数字段(RevenueIncludingTax)。我计算货币基值 = Math.ceil(RevenueIncludingTax)。当我尝试使用 Javascript 时,有时是正确的,有时是错误的。所以我尝试编写插件,并更新基值。虽然插件正在工作并在编码中更新值,但是当打开表单时它会显示错误消息并且不会更新基值。我怀疑我可以更新货币基础价值吗?
这是我的插件代码,
public class EstimatedRevenue : IPlugin
{
#region Class Level Variables
//IServiceProvider _serviceProvider;
//IOrganizationServiceFactory _serviceFactory = null;
//IOrganizationService _service = null;
//IPluginExecutionContext _context = null;
Entity _target = null;
Entity _preImage = null;
Entity _postImage = null;
Guid _currentUser;
decimal revinvtax;
#endregion
#region IPlugin Members
public void Execute(IServiceProvider serviceProvider)
{
try
{
string message = null;
IPluginExecutionContext _context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
#region Organization Services
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(_context.UserId);
#endregion
var ServiceContext = new OrganizationServiceContext(service);
_currentUser = _context.UserId;
message = _context.MessageName.ToLower();
if (message == "create")
{
if (_context.InputParameters.Contains("Target") && _context.InputParameters["Target"] != null)
_target = (Entity)_context.InputParameters["Target"];
if (_context.PreEntityImages.Contains("PreImage") && _context.PreEntityImages["PreImage"] != null)
_preImage = (Entity)_context.PreEntityImages["PreImage"];
if (_context.PostEntityImages.Contains("PostImage") && _context.PostEntityImages["PostImage"] != null)
_postImage = (Entity)_context.PostEntityImages["PostImage"];
if (_target.Attributes.Contains("hm_revenueincludingtax"))
{
revinvtax = (decimal)_target.Attributes["hm_revenueincludingtax"];
decimal ceilvalue = Math.Ceiling(revinvtax);
_target.Attributes["hm_estimatedrevenue_base"] = ceilvalue;
service.Update(_target);
}
}
if (message == "update")
{
if (_context.InputParameters.Contains("Target") && _context.InputParameters["Target"] != null)
_target = (Entity)_context.InputParameters["Target"];
if (_context.PreEntityImages.Contains("PreImage") && _context.PreEntityImages["PreImage"] != null)
_preImage = (Entity)_context.PreEntityImages["PreImage"];
if (_context.PostEntityImages.Contains("PostImage") && _context.PostEntityImages["PostImage"] != null)
_postImage = (Entity)_context.PostEntityImages["PostImage"];
if (_target.Attributes.Contains("hm_revenueincludingtax"))
{
revinvtax = (decimal)_target.Attributes["hm_revenueincludingtax"];
decimal ceilvalue = Math.Ceiling(revinvtax);
_target.Attributes["hm_estimatedrevenue_base"] = ceilvalue;
//service.Update(_target);
//ServiceContext.AddObject(_target);
//ServiceContext.SaveChanges();
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message, ex);
}
}
#endregion
}
这是我打开表单时的错误页面,