Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Supply chain | Supply Chain Management, Commerce
Unanswered

Update PreOperation Plugin Gets Called Multiple Times

(0) ShareShare
ReportReport
Posted on by 19

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;
                }
            }

  • Radi Profile Picture
    Radi 19 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,352 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;
                    }
                }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,430 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans