Dynamics CRM - C# Delete Plugin

purpose:

      In Dynamics CRM development, the most common is accomplished by Create Plugin Update Plugin and a variety of customization features, of course, there are also cases of use Delete Plugin herein, this simply talk about how to use Delete Plugin.

Scenes:

      In the CRM development, between the table and the table established by lookup field, now Consider the following scenario needs:

    A product such as the existence of Table A Table B and a sub-product, to establish a field in the Lookup Table A Table B, and Table A, there is a field to count the number of sub-products (i.e., how many record pointing in Table B own), then this time, when the (record delete) a sub-product is deleted, the statistics need to be a corresponding parent product updates, then you can use the delete Plugin to achieve this demand.

achieve:

      Code is relatively simple, it is determined whether or Delete Message context can when executed:

 1         public void Execute(IServiceProvider serviceProvider)
 2         {
 3             ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
 4             IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
 5             IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
 6             IOrganizationService service = factory.CreateOrganizationService(context.UserId);
 7 
 8             try
 9             {
10                 //TODO: Do stuff
11                 if (context.MessageName.Equals("Create"))
12                 {
13                     tracer.Trace("stage " + context.Stage);
14                     switch (context.Stage)
15                     {
16                         case 20:
17                             CreatePlugin(tracer, context, service, factory);
18                             break;
19                         case 40:
20                             break;
21                         default:
22                             break;
23                     }
24                 }
25                 else if (context.MessageName.Equals("Update"))
26                 {
27                     switch (context.Stage)
28                     {
29                         case 20:
30                             UpdatePlugin(tracer, context, service, factory);
31                             break;
32                         case 40:
33                             break;
34                         default:
35                             break;
36                     }
37                 }
38                 else if (context.MessageName.Equals("Delete"))//For Delete Plugin
39                 {
40                     switch (context.Stage)
41                     {
42                         case 10:
43                             DeletePlugin(tracer, context, service, factory);
44                             break;
45                     }
46                 }
47 
48             }
49             catch (Exception e)
50             {
51                 throw new InvalidPluginExecutionException(e.Message);
52             }
53 
54         }

 

      How to get all the information in the current Record Detele Request, the code is as follows, first get EntityReference the object, and then through the object to find the corresponding Record:

 1         private void DeletePlugin(ITracingService tracer, IPluginExecutionContext context, IOrganizationService service, IOrganizationServiceFactory factory)
 2         {
 3             tracer.Trace("Start DeletePlugin()");
 4             try
 5             {
 6                 EntityReference entity = ((EntityReference)context.InputParameters["Target"]);
 7                 Entities.new_entity = service.Retrieve(entity.LogicalName, entity.Id, new ColumnSet(true)).ToEntity<Entities.new_entity>();// Get Record all the information of the current, and then it can find the parent product according Lookup field . 8             }
 . 9 the catch (Exception EX)
 10             {
 . 11 IF (ex.GetType (). The Name == " InvalidPluginExecutionException " )
 12 is                 {
 13 is the throw new new InvalidPluginExecutionException (ex.Message + Environment.NewLine);
 14                 }
 15 the else 16                 {
 . 17 String the errorText = ex.Message.ToString ();
 18 is . 19 the while (ex.InnerException =! 
null) 20 { 21 errorText += ex.InnerException.Message; 22 ex = ex.InnerException; 23 } 24 throw new Exception("Error: " + errorText + "\n"); 25 } 26 } 27 tracer.Trace("End DeletePlugin()"); 28 }

Note: CRM in the implementation Delete Request, and the Create / Update Request is different than the current Record of the Entity object to get the Delete Context, but EntityReference object, if cast to Entity objects will complain, so remember to Execute method in this code deleted:

Entity entity = (Entity)context.InputParameters["Target"];

 

      Finally, registered under Delete Message can use 10 Stage (Pre-validation) or 20 Stage (Pre-operation) are generally recommended to use 10 Stage, because in Stage 20 may have deleted some relationship, such as the type of PartyList field, while 20 get Value.

 

   

Guess you like

Origin www.cnblogs.com/Sunny20181123/p/12007160.html