Common plugin for Deactivate / Delete all associated entity

Its walk-through for deactivating all associated (1:N) records. Steps for that are

  1. Create plugin
  2. Register and create steps

Create plugin

public class DeactivateAssociates : BaseClass
 {
 IOrganizationService service;

 public override void OnExecute(PluginExecution exeObject)
 {
 //'Get service reference
 service = exeObject.orgService;
 exeObject.tracingService.Trace("deactivation entity is started..");

 //check on create
 if ((exeObject.pluginContext.InputParameters.Contains("EntityMoniker")) && (exeObject.pluginContext.InputParameters["EntityMoniker"] is EntityReference))
 {
 exeObject.tracingService.Trace("geting entity context..");
 
 EntityReference targetEntity = (EntityReference)exeObject.pluginContext.InputParameters["EntityMoniker"];
 //get all associated Q-products
 exeObject.tracingService.Trace("geting asso. entity context..");
 EntityCollection etLines = GetETLines(targetEntity.Id);
 DeactivateEtLines(etLines);
 }
 else if ((exeObject.pluginContext.InputParameters.Contains("Target")) && (exeObject.pluginContext.InputParameters["Target"] is Entity))
 {
 exeObject.tracingService.Trace("geting entity context..");

 Entity targetEntity = (Entity)exeObject.pluginContext.InputParameters["Target"];
 //get all associated Q-products
 exeObject.tracingService.Trace("geting asso. entity context..");
 EntityCollection etLines = GetETLines(targetEntity.Id);

 foreach (Entity etLine in etLines.Entities)
 {
 deleteState(etLine.Id, etLine.LogicalName);
 }
 }
 
 }


 private void DeactivateEtLines(EntityCollection etLines)
 {
 foreach (Entity etLine in etLines.Entities)
 {
 updateState(etLine.Id, etLine.LogicalName);
 }
 }

 private EntityCollection GetETLines(Guid eTemlateId)
 {
 QueryExpression query = new QueryExpression("soft_estimatorline");
 query.ColumnSet = new ColumnSet(true);

 query.Criteria.AddCondition("soft_etTemplateid", ConditionOperator.Equal, eTemlateId);

 return service.RetrieveMultiple(query);
 }

 public void updateState(Guid entityId, string entityLogicalName)
 {
 SetStateRequest setState = new SetStateRequest();

 setState.EntityMoniker = new EntityReference();

 setState.EntityMoniker.Id = entityId;

 setState.EntityMoniker.Name = entityLogicalName;

 setState.EntityMoniker.LogicalName = entityLogicalName;

 setState.State = new OptionSetValue(1);
 setState.Status = new OptionSetValue(2);
 SetStateResponse setStateResponse = (SetStateResponse)service.Execute(setState);
 }

 public void deleteState(Guid entityId, string entityLogicalName)
 {
 service.Delete(entityLogicalName, entityId);
 }

 }


///common plugin things
public abstract class BaseClass : IPlugin
 {
 public void Execute(IServiceProvider serviceProvider)
 {
 PluginExecution obj = new PluginExecution();
 obj.pluginContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
 IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
 obj.orgService = serviceFactory.CreateOrganizationService(obj.pluginContext.UserId);
 obj.tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
 OnExecute(obj);
 }
 public abstract void OnExecute(PluginExecution exeObject);
 }
 public class PluginExecution
 {
 public IOrganizationService orgService { get; set; }
 public ITracingService tracingService { get; set; }
 public IPluginExecutionContext pluginContext { get; set; }
 }

Register it

02

Create Steps

We are done !!

 

It will also work for Delete …Just create delete step ..

Thanks.

 

Advertisements

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