插件中的Crm DateTime问题

时间:2012-06-27 02:33:57

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

我有一个与CRM DateTime问题有关的问题。机会表单中有自定义DateTime字段(投标提交日期),该字段在表单“仅限日期”格式中显示。 以及修改投标提交日期更改日期的其他字符串字段(投标日期)。让我们说...... 投标提交日期为29/06/2011 12:00:00 投标日期应为29/06/2012

我为Create Post-operation和Update Pre-operation创建了插件。我检索TenderSubDate.Day,Month和Year Crm时区是(格林威治标准时间+08:00)新加坡吉隆坡然后想改变(格林威治标准时间06:00)中部时间(美国和加拿大)。

问题在于,当我根据投标提交日期更新投标日期时,程序返回的日期小于或比投标子日期更重。让我们说..

First Secnario

投标提交日期为29/06/2012 12:00:00 am

计划返回 28/06/2012 (这是错的,应该是29/06/2012)

Second secnario

投标提交日期为1/08/2012 12:00:00 am

计划返回 32/07/2012 (这是错的,应该是2012年1月1日)

我应该在我的计划中做些什么。请给我一些想法。这是我的插件代码

public class TenderSubDateChange : 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;
    #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")//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"];

                DateTime hm_tenderdate;
                if (_target.Attributes.Contains("hm_tendersubmissiondate"))
                {
                    hm_tenderdate = (DateTime)_target.Attributes["hm_tendersubmissiondate"];
                    _target.Attributes["hm_tendersubdate"] = (hm_tenderdate.Day) + "/" + hm_tenderdate.Month + "/" + hm_tenderdate.Year;
                    service.Update(_target);
                }
            }
            if (message == "update")//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"];

                DateTime hm_tenderdate;
                if (_target.Attributes.Contains("hm_tendersubmissiondate"))
                {
                    hm_tenderdate = (DateTime)_target.Attributes["hm_tendersubmissiondate"];
                    _target.Attributes["hm_tendersubdate"] = (hm_tenderdate.Day) + "/" + hm_tenderdate.Month + "/" + hm_tenderdate.Year;
                }
            }
        }
        catch (Exception ex)
        {
            throw new InvalidPluginExecutionException(ex.Message, ex);
        }
    }
    #endregion
}

enter image description here

1 个答案:

答案 0 :(得分:4)

我之前遇到过这个问题并且让我发疯,解决方法是将UTC时间转换为当地时间

DateTime tenderDate = ((DateTime)target["new_tender"]).ToLocal();