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.
- List of contact Associated with Parent Account
#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
- Related child contact record status gets updated to In-Active. When Parent Account record gets In-Active
#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
- Related Contact Record status gets Updated to Active .
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
Hope This Help You . . .