2012-06-27 33 views
0

我有一個與CRM DateTime問題有關的問題。在機會表格中有自定義日期時間字段(投標提交日期),在表格上顯示'僅日期'格式。 和其他字符串字段(投標日期),修改投標提交日期更改時的日期。假設... 投標提交日期爲29/06/2011中午12:00:00 投標日期應爲29/06/2012插件中的Crm DateTime問題

我爲創建後操作和更新預操作創建插件。我檢索TenderSubDate.Day,Month和Year。
Crm時區爲(GMT + 08:00)吉隆坡,新加坡然後希望改變(GMT-06:00)中部時間(美國&加拿大)。

問題是,當我根據投標提交日期更新投標日期時,程序將返回比投標子日期少或超過一天的程序。讓說..

首先Secnario

投標提交日期爲2012/6/29上午12:00:00

程序返回28/06/2012(這是錯誤的,它應該是2012/6/29)

二secnario

遞交投標文件的日期是2012年1月8日上午12:00:00

程序返回32/07/2012(這是錯誤的,應該是2012年1月8日)

我應該在我的計劃去做。請給我一些想法。這是我在代碼插件

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

+0

另外請注意,您可以使用hm_tenderdate.Date獲取DateTime的日期組件(在午夜),而不是將它自己拼湊在一起。 –

回答

4

我面對這個問題前,讓我抓狂和解決方案是UTC時間轉換爲本地時間

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

+1(不得不使用.ToLocalTime()雖然) – sldev