在插件crm 2011中更新货币基础值

时间:2012-06-20 04:59:03

标签: dynamics-crm-2011 dynamics-crm-online

我有一个与crm money field(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
}

这是我打开表单时的错误页面, enter image description here

2 个答案:

答案 0 :(得分:2)

不要更新_base属性 - 其内部属性。而是更新您创建的属性(hm_estimatedrevenue)。它将采用主叫用户的货币,而_base将自动处理。

答案 1 :(得分:0)

_base如上所述,它存储在基础货币值中。因此,如果您在欧洲并在安装期间将基础货币设置为欧元,那么它将保存在_base中。如果输入250欧元,_base的值为250。

计算

hm_estimatedrevenue字段:_bases/exchange_rate以容纳多币种组织。

但您必须更新hm_estimatedrevenue,因为_basereadonly字段。

希望这会有所帮助。