Account is Deactivated
All
Related Contacts to be deactivated – Relationship to Account is remain intact
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace UltimaDisplay2016
{
public class DeactivateAccount : 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);
try
{
if ((context.InputParameters.Contains("EntityMoniker")) && (context.InputParameters["EntityMoniker"] is EntityReference))
{
var targetEntity = (EntityReference)context.InputParameters["EntityMoniker"];
var state = (OptionSetValue)context.InputParameters["State"];
if (targetEntity.LogicalName != "account")
{
return;
}
if (state.Value == 1)
{
Guid accountId = targetEntity.Id;
// to fetch contact details
EntityCollection entitycollection = getContactDetails(accountId, service);
//throw new InvalidProgramException("Contact Count " + entitycollection.TotalRecordCount.ToString());
foreach (Entity entity in entitycollection.Entities)
{
SetStateRequest contactsetStateReq = new SetStateRequest();
contactsetStateReq.EntityMoniker = new EntityReference(entity.LogicalName, entity.Id);
contactsetStateReq.State = new OptionSetValue(1);
contactsetStateReq.Status = new OptionSetValue(-1);
service.Execute(contactsetStateReq);
}
}
if (state.Value == 0)
{
Guid accountId = targetEntity.Id;
// to fetch contact details
EntityCollection entitycollection = getContactDetails(accountId, service);
//throw new InvalidProgramException("Contact Count " + entitycollection.TotalRecordCount.ToString());
foreach (Entity entity in entitycollection.Entities)
{
SetStateRequest contactsetStateReq = new SetStateRequest();
contactsetStateReq.EntityMoniker = new EntityReference(entity.LogicalName, entity.Id);
contactsetStateReq.State = new OptionSetValue(0);
contactsetStateReq.Status = new OptionSetValue(-1);
service.Execute(contactsetStateReq);
//entity.Attributes["parentcustomerid"] = null;
//service.Update(entity);
}
}
}
}
catch (Exception ex)
{
throw new InvalidProgramException(ex.Message);
}
}
// Fetching the record
public EntityCollection getContactDetails(Guid accountId, IOrganizationService service)
{
try
{
string fetchXML = @"<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>
<entity name='contact'>
<attribute name='contactid'/>
<filter type='and'>
<condition attribute='parentcustomerid' value='{0}' uitype='account' operator='eq'/>
</filter>
</entity>
</fetch>";
string fetch = string.Format(fetchXML, accountId);
EntityCollection collection = service.RetrieveMultiple(new FetchExpression(fetch));
return collection;
}
catch (Exception)
{
throw;
}
}
public EntityCollection getAccountDetails(Guid contactId, IOrganizationService service)
{
try
{
string fetchXML = @"<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>
<entity name='account'>
<attribute name='accountid'/>
<filter type='and'>
<condition attribute='primarycontactid' value='{0}' operator='eq'/>
</filter>
</entity>
</fetch>";
string fetch = string.Format(fetchXML, contactId);
EntityCollection collection = service.RetrieveMultiple(new FetchExpression(fetch));
return collection;
}
catch (Exception)
{
throw;
}
}
}
}
PLUGIN REGISTRATION STEPS:
Message : SetStateDynamicEntity
Primary Entity : account
Secondary Entity : none
Filtering Attributes: Message does not support Filtered Attributes
Event Pipeline of stage execution: Pre-operation
Execution Mode : Synchronous
Deployment: Server
No comments:
Post a Comment
Note: only a member of this blog may post a comment.