Sample: Convert date and time behavior

 

Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

This sample demonstrates how to use the ConvertDateAndTimeBehaviorRequest message to convert the UTC value to DateOnly value of the date and time attribute in Dynamics 365 that exist in the system before you changed the behavior of the attribute from UserLocal to DateOnly. More information: Convert behavior of existing date and time values in the database.

This sample is for Microsoft Dynamics CRM Online 2015 Update 1 and Microsoft Dynamics 365 (on-premises). This is available to download from Convert date and time behavior.

Prerequisites

  1. Have access to a Microsoft Dynamics CRM Online 2015 Update 1 or Microsoft Dynamics 365 organization.

  2. Have the System Administrator role in your Dynamics 365 organization to be able to use the ConvertDateAndTimeBehaviorRequest message that is demonstrated in this sample. Otherwise, this sample will fail.

  3. Download the DateTimeAttributeBehaviorVisual Studio project.

  4. Open the DateTimeAttributeBehavior Visual Studio project and install Microsoft.CrmSdk.CoreAssemblies version 7.1.0-preview NuGet Package. See Install NuGet packages later for instructions.

    After you install the NuGet packages, see Run the sample for instructions on how to run the sample and what to expect.

In This Topic

What this sample does

Install NuGet packages

Run the sample

What this sample does

  1. The ConvertDateandTimeBehavior class Run method checks if you are running the appropriate version of Dynamics 365 for the sample.

  2. The CreateRequiredRecords method creates the required records for the sample:

    1. Creates a sample date and time attribute for the Account entity with the UserLocal behavior.

    2. Creates a sample account record with March 31, 2015 11:00 PM UTC value in the new date and time attribute.

    3. Changes the behavior of the date and time attribute to DateOnly.

    4. Creates another sample account record with March 31, 2015 11:00 PM UTC value in the new date and time attribute. Because the attribute is now DateOnly, the time part is ignored and stored in the system as March 31, 2015 00:00:00.

    5. Retrieves the value in the date and time attribute in both the account records. Because the behavior is changed, both the values are displayed as March 31, 2015 00:00:00. However, the value in the first account record continues to be in the UTC (March 31, 2015 11:00 PM) in the database.

  3. Runs the ConvertDateAndTimeBehaviorRequest SDK message to create an async job for converting the UTC values in the date and time attribute to a DateOnly value. The conversion rule in the SDK message converts the value as per the India Standard Time (IST) time zone code in Dynamics 365 (190).

    
    ConvertDateAndTimeBehaviorRequest request = new ConvertDateAndTimeBehaviorRequest()
    {
        Attributes = new EntityAttributeCollection() 
                { 
                    new KeyValuePair<string, StringCollection>("account", new StringCollection() 
                    { "new_sampledatetimeattribute" }) 
                },
        ConversionRule = DateTimeBehaviorConversionRule.SpecificTimeZone.Value,
        TimeZoneCode = 190, // Time zone code for India Standard Time (IST) in CRM
        AutoConvert = false // Conversion must be done using ConversionRule
    };
    
    // Execute the request
    ConvertDateAndTimeBehaviorResponse response = (ConvertDateAndTimeBehaviorResponse)_serviceProxy.Execute(request);
    
  4. Retrieves the value in the date and time attribute in both the account records after the conversion. The UTC value in the first account record gets converted to April 1, instead of March 31 because the conversion happened as per the India Standard Time (IST) time zone. The value in the second account record is not affected because it was already a DateOnly value.

  5. Finally, the DeleteRequiredRecords method prompts you to delete the entity records and the custom date and time attribute created during the sample run:

    1. Type y, and press ENTER to delete the entity records and custom attribute from your organization.

    2. Type n, and press ENTER to leave the entity records and custom attribute in your organization. You will have to manually delete them later if you want to return to the original state.

Install NuGet packages

Use the following steps to install the required assemblies for this sample:

  1. Download this sample and extract the files.

  2. Navigate to the C# folder and open the DateTimeAttributeBehavior.sln file using Visual Studio.

  3. In Visual Studio, right-click the DateTimeAttributeBehavior project and chose Manage NuGet Packages.

  4. For the preview release, make sure to select Include Prerelease rather than Stable Only in the search criteria. Then search for “Microsoft Dynamics CRM 2015 SDK core assemblies”. Make sure you select the 7.1.0-preview version.

  5. Click Install. You’ll need to accept the license terms to complete installing this package.

Run the sample

  1. In Visual Studio, with the solution open and the necessary NuGet packages installed, press F5.

  2. If you have not previously run one of the Microsoft Dynamics 365 managed code samples before, you’ll need to enter information to run the code, otherwise enter the number for one of the Dynamics 365 servers you have previously set up.

    Prompt

    Description

    Enter a Dynamics 365 server name and port [crm.dynamics.com]

    Type the name of your Microsoft Dynamics 365 server. The default is Microsoft Dynamics 365 (online) (crm.dynamics.com) in North America.

    Example:
    crm5.dynamics.com

    Is this organization provisioned in Microsoft online services (y/n) [n]

    Type y if this is a Microsoft online services provisioned organization. Otherwise, type n.

    Enter domain\username

    Type your Microsoft account.

    Enter password

    Type your password. The characters will show as “*” in the window. Your password is securely saved in the Microsoft Credential Manager for later reuse.

    Specify an organization number (1-n) [1]

    From the list of organizations shown that you belong to, type the corresponding number. The default is 1, indicating the first organization in the list.

  3. The sample will perform the operations described in What this sample does and may prompt you with additional options.

  4. When the sample is complete, press ENTER to close the console window.

See Also

ConvertDateAndTimeBehaviorRequest
Behavior and format of the date and time attribute
Customize entity attribute metadata

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright