Search code examples
c#datetimedynamics-crm

How to convert DateTime field to Date in Dynamics CRM?


I am following this guide to update the DateTime fields to Date fields in Dynamics. When I run my code below, I don't receive an error, but I also don't see any changes to my date field.


Update: Based on Pawel Gradecki's answer and this article I checked the AsyncOperationBase table where I found the following message which seems to contradict the entity definition (shown in screenshot).

Parameters Conversion rule: SpecificTimeZone TimeZoneCode: 35 AutoConvert: False Error details: The specified attribute isn’t a Date and Time attribute with the Date Only behavior: Entity: ccseq_clientstatus, Attribute: ccseq_prospectstatusdate Attributes Processed

Entity Definition


using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace UpdateDateClientStatus1
{
class Program
{
    static void Main(string[] args)
    {
        var cred = new System.ServiceModel.Description.ClientCredentials();
        cred.Windows.ClientCredential.Domain = "DOMAIN";
        cred.Windows.ClientCredential.UserName = "USERNAME";
        cred.Windows.ClientCredential.Password = "PASSWORD";
        OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(new Uri("url"), null, cred,null);
        ConvertDateAndTimeBehaviorRequest request = new ConvertDateAndTimeBehaviorRequest()
        {
            Attributes = new EntityAttributeCollection()
        {
            // ccseq_clientstatus is the entity I am updating and ccseq_prospectstatusdate is the field I am updating
            new KeyValuePair<string, StringCollection>("ccseq_clientstatus", new StringCollection()  
            { "ccseq_prospectstatusdate" })    
        },
            ConversionRule = DateTimeBehaviorConversionRule.SpecificTimeZone.Value,
            TimeZoneCode = 035,// Time zone code for EST in CRM
            AutoConvert = false // Conversion must be done using ConversionRule
        };

        // Execute the request
        ConvertDateAndTimeBehaviorResponse response = (ConvertDateAndTimeBehaviorResponse)_serviceProxy.Execute(request);
    }
}
}

Solution

  • According to documentation:

    When you execute the ConvertDateAndTimeBehaviorRequest message, a system job (asynchronous operation) is created to run the conversion request. The ConvertDateAndTimeBehaviorResponse.JobId attribute in the message response displays the ID of the system job that is created as a result of the conversion request. After the system job completes, check the job details (AsyncOperation.Message) to view conversion details or errors, if any.

    So first of all, the conversion will be done after some time, because it's asynchronous, that's why you don't get any errors instantly. Secondly, if you want to check for errors, you should do this in AsyncOperation (System Job) of given JobId, simply look for the latest System Jobs or retrieve this system Job using SDK.