Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Commerce forum / Update PreOperation Pl...
Commerce forum
Unanswered

Update PreOperation Plugin Gets Called Multiple Times

Posted on by 2

We see that Update PreOperation Plugin gets called multiple times.

Scenario - Quote is updated by adding a Product Line. We see that there is only one update in audit log.

pastedimage1645884851993v1.png, But unable to figure out why there are many calls to our UpdateTestPlugin which is configured as

pastedimage1645885001089v2.png

UpdateTestPlugin Code is very simple one, to get the tracing service and add attributes from entity.

Code below.

ITracingService tracingService =
                 (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.  
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            // The InputParameters collection contains all the data passed in the message request.  
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.  
                Entity entity = (Entity)context.InputParameters["Target"];

                // Obtain the organization service reference which you will need for  
                // web service calls.  
                IOrganizationServiceFactory serviceFactory =
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {
                    // Plug-in business logic goes here.  
                    tracingService.Trace("inside update test plugin");
                    Entity entityFromContext = (Entity)context.InputParameters["Target"];
                    foreach (String key in entityFromContext.Attributes.Keys)
                    {
                        tracingService.Trace($"{key} " + "value " + entityFromContext.Attributes[key]);
                    }

                }

                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
                }

                catch (Exception ex)
                {
                    tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
                    throw;
                }
            }

Categories:
  • Radi Profile Picture
    Radi 2 on at
    RE: Update PreOperation Plugin Gets Called Multiple Times

    public void Execute(IServiceProvider serviceProvider)
    	{
    		ITracingService tracingService =
    			 (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    		// Obtain the execution context from the service provider.  
    		IPluginExecutionContext context = (IPluginExecutionContext)
    			serviceProvider.GetService(typeof(IPluginExecutionContext));
    		if (context.InputParameters.Contains("Target") &&
    			context.InputParameters["Target"] is Entity)
    		{
    			// Obtain the target entity from the input parameters.  
    			Entity entity = (Entity)context.InputParameters["Target"];
    
    			// Obtain the organization service reference which you will need for  
    			// web service calls.  
    			IOrganizationServiceFactory serviceFactory =
    				(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    			IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    			tracingService.Trace("inside update test plugin");
    			Entity entityFromContext = (Entity)context.InputParameters["Target"];
    			try
    			{
    				EntityCollection entityCollection = GetLineItems(entity, service);
    				Money money = new Money();
    				decimal d = 0;
    				List updateLineEntityList = new List();
    				foreach (Entity lineEntity in entityCollection.Entities)
    				{
    					lineEntity["tax"] = new Money(12);
    					Money money1 = (Money)lineEntity["amount"];
    					lineEntity["extendedamount"] = new Money(12   money1.Value);
    					updateLineEntityList.Add(lineEntity);
    					d  = 12;
    				}
    				entityFromContext.RelatedEntities.Add(new Relationship("quote_details"), new EntityCollection(updateLineEntityList));
    				entityFromContext["totaltax"] = new Money(d   5);
    			}
    			catch (FaultException ex)
    			{
    				throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
    			}
    			catch (Exception ex)
    			{
    				throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
    			}
    		}
    	}
    
    	private EntityCollection GetLineItems(Entity entity, IOrganizationService service)
    	{
    		QueryExpression query = new QueryExpression(Tables.QUOTE_PRODUCT);
    		string[] columns = { "extendedamount","tax","amount" };
    		var columnList = new List();
    		columnList.AddRange(columns);
    		columnList.Add("quoteid");
    		query.ColumnSet = new ColumnSet(columnList.ToArray());
    		FilterExpression filter = new FilterExpression();
    		filter.AddCondition("quoteid", ConditionOperator.Equal, entity.Id);
    		query.Criteria = filter;
    		EntityCollection entityCollection = service.RetrieveMultiple(query);
    		return entityCollection;
    	}
    }

    Hi,

    Thank for looking into this.

    Here is the updated code. I added this code as a plugin, and ran it as a PreOperation Plugin On the Quote Update.

    What I wanted to achieve is below -

    On each Quote Save, will get all the quotedetails for the given quote,

    for each quote detail

    set quotedetail tax as 12 and extendedamount as amount 12

    on quote which I got from context, set the totaltaxamount as quotedetailcount*12 5

    There are two issues, I am seeing with this -

    1. This plugin is getting called multiple times

    2. I couldn't able to save the quotedetail records using the relatedEntities. How I can do this.

    Appreciate any help on this. Thanks.

  • WillWU Profile Picture
    WillWU 22,344 on at
    RE: Update PreOperation Plugin Gets Called Multiple Times

    Hi partner,

    Please format the code to make it easy to read.

    Where did you put your code?

    ITracingService tracingService =
                     (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    
                // Obtain the execution context from the service provider.  
                IPluginExecutionContext context = (IPluginExecutionContext)
                    serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                // The InputParameters collection contains all the data passed in the message request.  
                if (context.InputParameters.Contains("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters.  
                    Entity entity = (Entity)context.InputParameters["Target"];
    
                    // Obtain the organization service reference which you will need for  
                    // web service calls.  
                    IOrganizationServiceFactory serviceFactory =
                        (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                    try
                    {
                        // Plug-in business logic goes here.  
                        tracingService.Trace("inside update test plugin");
                        Entity entityFromContext = (Entity)context.InputParameters["Target"];
                        foreach (String key in entityFromContext.Attributes.Keys)
                        {
                            tracingService.Trace($"{key} "   "value "   entityFromContext.Attributes[key]);
                        }
    
                    }
    
                    catch (FaultException ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
                    }
    
                    catch (Exception ex)
                    {
                        tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
                        throw;
                    }
                }

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans