private void btnConvertDateOnly_Click(object sender, EventArgs e) { if (DialogResult.Yes == MessageBox.Show("This action will update data on all records for selected attributes in connected organization.\n\nUse with caution!\n\nContinue?", "Convert data", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)) { var tz = cmbCountryCodes.SelectedItem as CRMTimeZone; var req = new ConvertDateAndTimeBehaviorRequest(); req.Attributes = GetSelectedEntityAttributes(); req.ConversionRule = cmbConversionRule.SelectedItem.ToString(); if (tz != null) { req.TimeZoneCode = tz.TimeZoneCode; } req.AutoConvert = cbAutoConvert.Checked; //Execute var resp = Service.Execute(req) as ConvertDateAndTimeBehaviorResponse; linkConvertJob.Text = resp.JobId.ToString(); MessageBox.Show("Conversion is performed asynchronously.\nClick the link on the form to open system job and verify the results.", "Convert data", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
/// <summary> /// Create and configure the organization service proxy. /// Create a date/time attribute for account entity with UserLocal behavior. /// Create an account record. /// Retrieve the value in the new date/time attribute. /// Update attribute to set the behavior to DateOnly. /// Create another account record. /// Retrieve both the account records to compare the date value retrieved. /// Use the "ConvertDateandTimeRequest" message to change the behavior for the /// existing records. /// Optionally delete/revert any attributes /// that were created/changed for this sample. /// </summary> /// <param name="serverConfig">Contains server connection information.</param> /// <param name="promptForDelete">When True, the user will be prompted to delete all /// created entities.</param> public void Run(ServerConnection.Configuration serverConfig, bool promptForDelete) { try { // Connect to the Organization service. // The using statement assures that the service proxy will be properly disposed. using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials)) { // This statement is required to enable early-bound type support. _serviceProxy.EnableProxyTypes(); // Check if you are running the correct version of CRM. // This sample requires that the CRM version be 7.1.0.xxxx or later. RetrieveVersionRequest crmVersionReq = new RetrieveVersionRequest(); RetrieveVersionResponse crmVersionResp = (RetrieveVersionResponse)_serviceProxy.Execute(crmVersionReq); if (String.CompareOrdinal("7.1.0.0", crmVersionResp.Version) < 0) { // Create required records for the sample. CreateRequiredRecords(); // Use the ConvertDateandTimeBehaviorRequest SDK message to change // the behavior of the date and time values in the custom attribute // (new_SampleDateTimeAttribute) for the account entity. 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); Console.WriteLine("***************************************"); Console.WriteLine("Executed the ConvertDateAndTimeBehaviorRequest SDK message.\n"); // Wait for two seconds to let the async job be created System.Threading.Thread.Sleep(2000); if (response.JobId != null) { Console.WriteLine("An async job created with ID: {0}", response.JobId.ToString()); } // Retrieve the job completion details based on the Job ID ColumnSet cs = new ColumnSet("statecode", "statuscode", "friendlymessage", "message"); Console.WriteLine("Waiting for the async job to complete...\n"); AsyncOperation crmAsyncJob = new AsyncOperation(); while (response.JobId != null && waitCount > 0) { // Check to see if the async operation is complete crmAsyncJob = (AsyncOperation)_serviceProxy.Retrieve(AsyncOperation.EntityLogicalName, response.JobId, cs); if (crmAsyncJob.StateCode.HasValue && crmAsyncJob.StateCode.Value == AsyncOperationState.Completed && crmAsyncJob.StatusCode.Value == (int)asyncoperation_statuscode.Succeeded) { waitCount = 0; Console.WriteLine("The async job is complete.\n"); Console.WriteLine("****************************"); Console.WriteLine(crmAsyncJob.FriendlyMessage); Console.WriteLine("****************************"); Console.WriteLine(crmAsyncJob.Message); Console.WriteLine("****************************\n"); // Retrieve both the account records created earlier to check the date value Console.WriteLine("Retrieving the date and time values after the conversion...\n"); // Create a column set to define which attributes should be retrieved. ColumnSet attributes = new ColumnSet(new string[] { "name", "new_sampledatetimeattribute" }); Account retrievedAccount1 = (Account)_serviceProxy.Retrieve(Account.EntityLogicalName, _account1ID, attributes); Account retrievedAccount2 = (Account)_serviceProxy.Retrieve(Account.EntityLogicalName, _account2ID, attributes); Console.WriteLine("'{0}' is: {1}", retrievedAccount1.GetAttributeValue<String>("name"), retrievedAccount1.GetAttributeValue<DateTime>("new_sampledatetimeattribute")); Console.WriteLine("'{0}' is: {1}\n", retrievedAccount2.GetAttributeValue<String>("name"), retrievedAccount2.GetAttributeValue<DateTime>("new_sampledatetimeattribute")); Console.WriteLine("The behavior converted to DateOnly for account record ('Sample Account 1')\nbased on the specified conversion rule.\n"); Console.WriteLine("No changes to 'Sample Account 2' because it was already DateOnly.\n"); Console.WriteLine("***************************************\n"); } else { waitCount--; System.Threading.Thread.Sleep(1000); } } // If the async job is taking tool long to process, // inform the user about the same. if (waitCount == 0 && crmAsyncJob.StateCode.Value != (AsyncOperationState.Completed)) { Console.WriteLine("The async job is taking too long to complete. Aborting the sample."); } // Prompt the user to delete the records and attribute created by the sample. DeleteRequiredRecords(promptForDelete); } else { Console.WriteLine("This sample cannot be run against the current version of CRM."); Console.WriteLine("Upgrade your CRM instance to the latest version to run this sample."); } }