Thursday, 29 July 2021

Write a common plugin for Create/Update/Delete events in Dynamics CRM

 If you want to write a common plugin for Create/Update/Delete events in Dynamics CRM 2011 then only you need to take care of “context.InputParameters[“Target”]”. In case of Create/Update event “context.InputParameters[“Target”] is Entity” and in case of Delete event “context.InputParameters[“Target”] is EntityReference”. Below is the sample code for the same:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public void Execute(IServiceProvider serviceProvider)
{
     // Obtain the execution context from the service provider.
     IPluginExecutionContext context = (IPluginExecutionContext)
     serviceProvider.GetService(typeof(IPluginExecutionContext));
     // Obtain the organization service reference.
     IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
     IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
     // The InputParameters collection contains all the data passed in the message request.
     if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
     {
          if (context.MessageName == "Create")
          {
               //Code to be executed during Create event of an entity
          }
          else if (context.MessageName == "Update")
          {
               //Code to be executed during Update event of an entity
          }
      }
      else if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
      {
          if (context.MessageName == "Delete")
          {
              //Code to be executed during Delete event of an entity
          }
      }
}

No comments:

Post a Comment

Introduction to Dynamics 365 CE Data Migration using ADF

Dynamics 365 CE Data Migration using ADF can be necessary for various reasons, such as archiving historical data, integrating with other sys...