Udostępnij za pośrednictwem


Sample: Convert date and time behavior

 

Applies To: Dynamics CRM 2015

This sample demonstrates how to use the ConvertDateAndTimeBehaviorRequest message to convert the UTC value to DateOnly value of the date and time attribute in CRM that exist in the system before you changed the behavior of the attribute from UserLocal to DateOnly. More information: 17014973-417b-4f53-8991-88ccc0096cfc#Convert.

This sample is for Microsoft Dynamics CRM Online 2015 Update 1. This is available to download from Convert date and time behavior.

Prerequisites

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

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

  3. Download the DateTimeAttributeBehavior Visual Studio project.

  4. Open the DateTimeAttributeBehavior Visual Studio project and install Microsoft.CrmSdk.CoreAssemblies version 7.1.0-preview NuGet Package. See 5aac6430-5d98-4e50-94f9-c2f6ce5cee62#BKMK_installNuget later for instructions.

    After you install the NuGet packages, see 5aac6430-5d98-4e50-94f9-c2f6ce5cee62#BKMK_runSample for instructions on how to run the sample and what to expect.

In This Topic

e54f194e-3148-4bcc-8fc3-9af7e5f03995#BKMK_WhatThisSampleDoes

e54f194e-3148-4bcc-8fc3-9af7e5f03995#BKMK_installNuget

e54f194e-3148-4bcc-8fc3-9af7e5f03995#BKMK_runSample

What this sample does

  1. The ConvertDateandTimeBehavior class Run method checks if you are running the appropriate version of CRM 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 CRM (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 choose 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 CRM managed code samples before, you’ll need to enter information to run the code, otherwise enter the number for one of the CRM servers you have previously set up.

    Prompt

    Description

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

    Type the name of your Microsoft Dynamics CRM server. The default is Microsoft Dynamics CRM 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 5aac6430-5d98-4e50-94f9-c2f6ce5cee62#BKMK_WhatThisSampleDoes 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

© 2016 Microsoft. All rights reserved. Copyright