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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Regarding the Update in the OrderProduct Table(Sales Order)

(0) ShareShare
ReportReport
Posted on by 35

I want to Update a Custom Fields Value in Sales Order Details i.e Stock Weight, I want to fetch the relative Stock Weight from the Products Table and then set it into the Custom Field on the Order Products Table. I want to do it with Plugins 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling;

namespace WeightCalculation
{
public class Weight : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

if (context.InputParameters.Contains("Target") & context.InputParameters["Target"] is Entity &context.Depth < 2 )
{
Entity Orderline = context.InputParameters["Target"] as Entity;

EntityReference ExistinProdcut = (EntityReference)Orderline["productid"];
tracingService.Trace("2"+ExistinProdcut.Id);
Entity Product = new Entity("product");
tracingService.Trace("3");
Product.Id = ExistinProdcut.Id;
Orderline["cr873_stockweight"] = Product.GetAttributeValue<Decimal>("stockweight");
service.Update(Orderline);

}


}
}
}

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    You're missing the querying of the record itself. Here is the how your code can look like:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Xrm.Tooling;
    
    namespace WeightCalculation {
      public class Weight: IPlugin {
        public void Execute(IServiceProvider serviceProvider) {
          IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof (IOrganizationServiceFactory));
          ITracingService tracingService = (ITracingService) serviceProvider.GetService(typeof (ITracingService));
    
          IPluginExecutionContext context = (IPluginExecutionContext) serviceProvider.GetService(typeof (IPluginExecutionContext));
          IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
          if (context.InputParameters.Contains("Target") & context.InputParameters["Target"] is Entity & context.Depth < 2) {
            Entity Orderline = context.InputParameters["Target"] as Entity;
    
            EntityReference ExistinProdcut = (EntityReference) Orderline["productid"];
            tracingService.Trace("2"   ExistinProdcut.Id);
    
    	var Product = service.Retreive("product", ExistinProdcut.Id, new ColumnSet("stockweight"));
    
            Orderline["cr873_stockweight"] = Product.GetAttributeValue("stockweight");
            service.Update(Orderline);
    
          }
    
        }
      }
    }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 185 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 110

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 69 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans