1
votes

I have a question relating to CRM DateTime problem. In opportunity form has custom DateTime field(Tender submission Date) that shows on form 'Date only' Format. And other string field (Tender Date) that modifies date when Tender submission date changes. Let say... Tender submission date is 29/06/2011 12:00:00 Tender Date should be 29/06/2012

I create the plug-in for Create Post-operation and Update Pre-operation. I retrieves TenderSubDate.Day, Month and Year.
Crm Time zone is (GMT+08:00) Kuala Lumpur,Singapore then want to change (GMT-06:00) Central Time(US & Canada).

The problem is that when i update Tender date based on Tender submission date, the program return one day less than or grater than Tender sub date. Let say..

First Secnario

Tender submission date is 29/06/2012 12:00:00am

Program returns 28/06/2012(it's wrong and it should be 29/06/2012)

Second secnario

Tender submission date is 1/08/2012 12:00:00am

Program returns 32/07/2012(it's wrong and it should be 1/08/2012)

what should i do in my program. please give me some idea. Here is my plug in code

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
Also note, you could use hm_tenderdate.Date to get the date component of the DateTime (at midnight) instead of piecing it together yourself.John M. Wright

1 Answers

4
votes

I faced this issue before and drove me crazy and the solution is to convert UTC time to local time

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