In this post, I am going to describe how to retrieve Sales Order Product items for a particular Product Bundle. If a bundle has 10 products and this bundle is added in sales order lines. This will make 11 sales order lines one header with bundle title and rest as bundle products.
So if we want to get quantity of all bundle items excluding header bundle item then we need to find the relationship and data values insert by Dynamics 365 in backed.
In below plugin, I am demonstrating how to get the bundle item id (sales order line item). Then we can query (fetchxml / query expression) to retrieve all lines having the bundle id in Parent Bundle Id.
Plugin (In this plugin I am showing how to retrieve bundle id)
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test.Plugins
{
public class BundleProductQtyCheck : IPlugin
{
bool IsDebug = false;
public void Execute(IServiceProvider serviceProvider)
{
IsDebug = false;
Entity DebugEntity = new Entity("new_plugindebug");
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
string DebugDesc = " Started...";
try
{
string strGcTest = "";
string NEWLINE = "";
strGcTest = " Plugin Debug started... at " + DateTime.Now + NEWLINE;
string entityName = "";
Entity OrderLine = null;
Entity RetrievedOrderLine = null;
OrderLine = (Entity)context.InputParameters["Target"];
entityName = OrderLine.LogicalName;
if (context.MessageName == "Update")
{
if (OrderLine.Attributes.Contains("shipto_telephone"))
return;
RetrievedOrderLine = service.Retrieve(OrderLine.LogicalName, OrderLine.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
if (RetrievedOrderLine.Attributes.Contains("parentbundleid")) {
OrderLine["shipto_telephone"] = ((Guid)RetrievedOrderLine["parentbundleid"]).ToString();
service.Update(OrderLine);
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
tracingService.Trace(ex.Message);
if (IsDebug)
{
DebugEntity["new_name"] = this.ToString();
DebugEntity["new_description"] = DebugDesc + "\n" + DebugEntity.GetAttributeValue<string>("new_description") + ex.Message + "\n" + ex.ToString();
service.Create(DebugEntity);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test.Plugins
{
public class BundleProductQtyCheck : IPlugin
{
bool IsDebug = false;
public void Execute(IServiceProvider serviceProvider)
{
IsDebug = false;
Entity DebugEntity = new Entity("new_plugindebug");
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
string DebugDesc = " Started...";
try
{
string strGcTest = "";
string NEWLINE = "";
strGcTest = " Plugin Debug started... at " + DateTime.Now + NEWLINE;
string entityName = "";
Entity OrderLine = null;
Entity RetrievedOrderLine = null;
OrderLine = (Entity)context.InputParameters["Target"];
entityName = OrderLine.LogicalName;
if (context.MessageName == "Update")
{
if (OrderLine.Attributes.Contains("shipto_telephone"))
return;
RetrievedOrderLine = service.Retrieve(OrderLine.LogicalName, OrderLine.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
if (RetrievedOrderLine.Attributes.Contains("parentbundleid")) {
OrderLine["shipto_telephone"] = ((Guid)RetrievedOrderLine["parentbundleid"]).ToString();
service.Update(OrderLine);
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
tracingService.Trace(ex.Message);
if (IsDebug)
{
DebugEntity["new_name"] = this.ToString();
DebugEntity["new_description"] = DebugDesc + "\n" + DebugEntity.GetAttributeValue<string>("new_description") + ex.Message + "\n" + ex.ToString();
service.Create(DebugEntity);
}
}
}
}
}
㉺㉼㉴㉳㉽㉾㈕㈔㈆㈅㈄㈄㈃㈁㈀㉤ Ali Hamza Wadood Microsoft Dynamics CRM Developer | Software Engineer - Microsoft Technologies(Asp.Net, Asp.Net MVC) LinkedIn