web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
FastTrack for Dynamics 365 forum
Suggested Answer

Update PreOperation Plugin - Update a field and also RelatedEntites

(1) ShareShare
ReportReport
Posted on by 19

Hi,

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.

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("quotedetail");
		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;
	}
}

I have the same question (0)
  • Suggested answer
    Abdul Wahab Profile Picture
    12,119 Moderator on at

    Hi Radi,

    Can you please let us know the version of your instance? I need to check whether we are able to use rollup or calculated columns.

    Let me know if I can provide more details for you.

    Thanks

    Regards,

    Abdul Wahab

    Power Platform/Customer Engagement Developer/Lead/Solution Architecture/Project Manager

    Direct/WhatsApp:+923323281237

    E-mail: abdulwahabubit@outlook.com

    Skype: abdul.wahabubit

    Linkedin: www.linkedin.com/.../

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > FastTrack for Dynamics 365

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 4 Super User 2026 Season 1

#2
CU12010503-0 Profile Picture

CU12010503-0 2

#2
RK-16042134-0 Profile Picture

RK-16042134-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans