Hi all ,

In this article I would like to explain how to De-Activate child records (Related records) when parent record status gets changed.

  • In my Scenario I am taking Account entity, which consists of related Contact record in Left navigation.
  • I would like to De-Activate all list of contact associated with Account.

Cascade Deactivates Record CRM 2011

  • List of contact Associated with Parent Account

2

#region  De-Activate Related Contact Associated with Account Record

    //Note for  Active record  state value = 1 .
   // For In - Active record  State value = 0 .

  /// Check weather state value == 1.
         if (state.Value == 1)
            {
                    /// querry expression to retrieve related Contact associated to Account.
                    QueryExpression contact = new QueryExpression { EntityName = "contact", ColumnSet = new ColumnSet("contactid", "parentcustomerid") };
                    contact.Criteria.AddCondition("parentcustomerid", ConditionOperator.Equal, Account.Id);

                    // Check weather statecode of the record are Active
                    contact.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
                    EntityCollection Retrievecontact = service.RetrieveMultiple(contact);

                    if (Retrievecontact.Entities.Count > 0)
                    {

                        foreach (var a in Retrievecontact.Entities)
                        {
                            SetStateRequest contactsetStateReq = new SetStateRequest();
                            contactsetStateReq.EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
                            contactsetStateReq.State = new OptionSetValue(1);
                            contactsetStateReq.Status = new OptionSetValue(-1);
                            service.Execute(contactsetStateReq);
                        }

                    }

                }
    #endregion
  • De – Active Parent Record

3

  • Related child contact record status gets updated to In-Active. When Parent Account record gets In-Active

5

#region Re-Activate In-Active Contact Records

   /// Check weather state value == 0. For In active records
        if (state.Value == 0)
          {
    /// querry expression to retrieve related Contact associated to Account.
       QueryExpression contact = new QueryExpression { EntityName = "contact", ColumnSet = new ColumnSet("contactid", "parentcustomerid") };
         contact.Criteria.AddCondition("parentcustomerid", ConditionOperator.Equal, Account.Id);

      // Check weather statecode of the record are In-Active
        contact.Criteria.AddCondition("statecode", ConditionOperator.Equal, 1);
          EntityCollection Retrievecontact = service.RetrieveMultiple(contact);

       if (Retrievecontact.Entities.Count > 0)
                    {

                        foreach (var a in Retrievecontact.Entities)
                        {
                            SetStateRequest contactsetStateReq = new SetStateRequest();
                            contactsetStateReq.EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
                            contactsetStateReq.State = new OptionSetValue(0);
                            contactsetStateReq.Status = new OptionSetValue(-1);
                            service.Execute(contactsetStateReq);
                        }
          }
          }
  #endregion
  • Re- Activate Account Record to Active State

6

  • Related Contact Record status gets Updated to Active .

8

Plugin Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//CRM SDK Namespace
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;

namespace Sample
{
    public class Cascade : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)
              serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

            if (context.InputParameters.Contains("EntityMoniker") && context.InputParameters["EntityMoniker"] is EntityReference)
            {

                //Parent Entity (Account)
                EntityReference Account = (EntityReference)context.InputParameters["EntityMoniker"];

                //Capturing record  State & Status
                OptionSetValue state = (OptionSetValue)context.InputParameters["State"];
                OptionSetValue status = (OptionSetValue)context.InputParameters["Status"];

                #region De-Activate Related Contact Associated with Account Record

                //Note for  Active record  state value = 1 .
                // For In - Active record  State value = 0 .

                /// Check weather state value == 1.
                if (state.Value == 1)
                {
                    /// querry expression to retrieve related Contact associated to Account.
                    QueryExpression contact = new QueryExpression { EntityName = "contact", ColumnSet = new ColumnSet("contactid", "parentcustomerid") };
                    contact.Criteria.AddCondition("parentcustomerid", ConditionOperator.Equal, Account.Id);

                    // Check weather statecode of the record are Active
                    contact.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
                    EntityCollection Retrievecontact = service.RetrieveMultiple(contact);

                    if (Retrievecontact.Entities.Count > 0)
                    {

                        foreach (var a in Retrievecontact.Entities)
                        {
                            SetStateRequest contactsetStateReq = new SetStateRequest();
                            contactsetStateReq.EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
                            contactsetStateReq.State = new OptionSetValue(1);
                            contactsetStateReq.Status = new OptionSetValue(-1);
                            service.Execute(contactsetStateReq);
                        }

                    }

                }
                #endregion

                ///<Re Activate Relate record when Parent record status gets Activated>

                #region Re-Activate In-Active Contact Records

                /// Check weather state value == 0. For In active records
                if (state.Value == 0)
                {
                    /// querry expression to retrieve related Contact associated to Account.
                    QueryExpression contact = new QueryExpression { EntityName = "contact", ColumnSet = new ColumnSet("contactid", "parentcustomerid") };
                    contact.Criteria.AddCondition("parentcustomerid", ConditionOperator.Equal, Account.Id);

                    // Check weather statecode of the record are In-Active
                    contact.Criteria.AddCondition("statecode", ConditionOperator.Equal, 1);
                    EntityCollection Retrievecontact = service.RetrieveMultiple(contact);

                    if (Retrievecontact.Entities.Count > 0)
                    {

                        foreach (var a in Retrievecontact.Entities)
                        {
                            SetStateRequest contactsetStateReq = new SetStateRequest();
                            contactsetStateReq.EntityMoniker = new EntityReference(a.LogicalName, new Guid(a.Id.ToString()));
                            contactsetStateReq.State = new OptionSetValue(0);
                            contactsetStateReq.Status = new OptionSetValue(-1);
                            service.Execute(contactsetStateReq);
                        }

                    }

                }
                #endregion
            }
        }
    }
}

  • Register your plugin Message = SetStateDynamicEntity
  • Primary Entity = Account
  • Post Operation

9

Hope This Help You . . . 

Advertisements

One thought on “Cascade Deactivate Related Record using C# plugin CRM 2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s