Created
December 17, 2021 17:56
-
-
Save melamriD365/8124c50fcee602548d935a20f3a45139 to your computer and use it in GitHub Desktop.
Dynamics 365 For Sales: How to merge Accounts if the subordinate record is associated with one or more active quotes?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <copyright file="PreOperationAccountMerge.cs" company=""> | |
// Copyright (c) 2021 All Rights Reserved | |
// </copyright> | |
// <author></author> | |
// <date>12/17/2021 6:04:07 PM</date> | |
// <summary>Implements the PreOperationAccountMerge Plugin.</summary> | |
// <auto-generated> | |
// This code was generated by a tool. | |
// Runtime Version:4.0.30319.1 | |
// </auto-generated> | |
using System; | |
using System.ServiceModel; | |
using Microsoft.Xrm.Sdk; | |
using Microsoft.Xrm.Sdk.Query; | |
namespace MergePoc.Plugins | |
{ | |
/// <summary> | |
/// PreOperationAccountMerge Plugin. | |
/// </summary> | |
public class PreOperationAccountMerge: PluginBase | |
{ | |
/// <summary> | |
/// Initializes a new instance of the <see cref="PreOperationAccountMerge"/> class. | |
/// </summary> | |
/// <param name="unsecure">Contains public (unsecured) configuration information.</param> | |
/// <param name="secure">Contains non-public (secured) configuration information. | |
/// When using Microsoft Dynamics 365 for Outlook with Offline Access, | |
/// the secure string is not passed to a plug-in that executes while the client is offline.</param> | |
public PreOperationAccountMerge(string unsecure, string secure) | |
: base(typeof(PreOperationAccountMerge)) | |
{ | |
// TODO: Implement your custom configuration handling. | |
} | |
/// <summary> | |
/// Main entry point for he business logic that the plug-in is to execute. | |
/// </summary> | |
/// <param name="localContext">The <see cref="LocalPluginContext"/> which contains the | |
/// <see cref="IPluginExecutionContext"/>, | |
/// <see cref="IOrganizationService"/> | |
/// and <see cref="ITracingService"/> | |
/// </param> | |
/// <remarks> | |
/// For improved performance, Microsoft Dynamics 365 caches plug-in instances. | |
/// The plug-in's Execute method should be written to be stateless as the constructor | |
/// is not called for every invocation of the plug-in. Also, multiple system threads | |
/// could execute the plug-in at the same time. All per invocation state information | |
/// is stored in the context. This means that you should not use global variables in plug-ins. | |
/// </remarks> | |
protected override void ExecuteCdsPlugin(ILocalPluginContext localContext) | |
{ | |
if (localContext == null) | |
{ | |
throw new InvalidPluginExecutionException(nameof(localContext)); | |
} | |
// Obtain the tracing service | |
ITracingService tracingService = localContext.TracingService; | |
try | |
{ | |
// Obtain the execution context from the service provider. | |
IPluginExecutionContext context = (IPluginExecutionContext)localContext.PluginExecutionContext; | |
// Obtain the organization service reference for web service calls. | |
IOrganizationService currentUserService = localContext.CurrentUserService; | |
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference) | |
{ | |
EntityReference entityReferenceMaster = (EntityReference)context.InputParameters["Target"]; | |
Guid GuidSubOrdinate = (Guid)context.InputParameters["SubordinateId"]; | |
//Define Condition Values | |
//active quotes | |
var query_statecode = 1; | |
//related to the subAccount | |
var query_msdyn_account = GuidSubOrdinate; | |
//Instantiate QueryExpression query | |
var query = new QueryExpression("quote"); | |
//!Add all columns to query.ColumnSet | |
query.ColumnSet.AllColumns = false; | |
// Define filter query.Criteria | |
query.Criteria.AddCondition("statecode", ConditionOperator.Equal, query_statecode); | |
query.Criteria.AddCondition("msdyn_account", ConditionOperator.Equal, query_msdyn_account); | |
var quotes = currentUserService.RetrieveMultiple(query); | |
foreach (var quote in quotes.Entities) | |
{ | |
//move accounts from the subAccount to the Master Account | |
quote["msdyn_account"] = new EntityReference("account", entityReferenceMaster.Id); | |
quote["customerid"] = new EntityReference("account", entityReferenceMaster.Id); | |
//Update Before the merge validation | |
currentUserService.Update(quote); | |
} | |
} | |
} | |
// Only throw an InvalidPluginExecutionException. Please Refer https://go.microsoft.com/fwlink/?linkid=2153829. | |
catch (Exception ex) | |
{ | |
tracingService?.Trace("An error occurred executing Plugin MergePoc.Plugins.PreValidationaccountMerge : {0}", ex.ToString()); | |
throw new InvalidPluginExecutionException("An error occurred executing Plugin MergePoc.Plugins.PreValidationaccountMerge .", ex); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment