private void PopulatePlanPackageList() { PlanPackageList.Items.Clear(); var supportRequest = XrmContext.CreateQuery("adx_supportrequest") .FirstOrDefault(sr => sr.GetAttributeValue <Guid>("adx_supportrequestid") == CurrentStepEntityID); if (supportRequest == null) { throw new ApplicationException(string.Format("Could not find support request record with id equal to {0}", CurrentStepEntityID)); } var supportRequestProductReference = supportRequest.GetAttributeValue <EntityReference>("adx_product"); var parentProduct = supportRequestProductReference == null ? null : XrmContext.CreateQuery("product").FirstOrDefault(pp => pp.GetAttributeValue <Guid>("productid") == supportRequestProductReference.Id); IQueryable <Entity> supportProducts; if (parentProduct != null) { supportProducts = from product in XrmContext.CreateQuery("product") join supportedset in XrmContext.CreateQuery("adx_supportedproduct_productplan") on product.GetAttributeValue <Guid>("productid") equals supportedset.GetAttributeValue <Guid>("productidone") join supportedProduct in XrmContext.CreateQuery("product") on supportedset.GetAttributeValue <Guid>("productidtwo") equals supportedProduct.GetAttributeValue <Guid>("productid") where product.GetAttributeValue <OptionSetValue>("statecode") != null && product.GetAttributeValue <OptionSetValue>("statecode").Value == 0 where product.GetAttributeValue <Guid>("productid") == parentProduct.GetAttributeValue <Guid>("productid") where supportedProduct.GetAttributeValue <OptionSetValue>("producttypecode") != null && supportedProduct.GetAttributeValue <OptionSetValue>("producttypecode").Value == (int)ProductTypeCode.SupportPlan select supportedProduct; } else { supportProducts = XrmContext.CreateQuery("product").Where(p => p.GetAttributeValue <OptionSetValue>("statecode") != null && p.GetAttributeValue <OptionSetValue>("statecode").Value == 0 && p.GetAttributeValue <OptionSetValue>("producttypecode") != null && p.GetAttributeValue <OptionSetValue>("producttypecode").Value == (int)ProductTypeCode.SupportPlan); } foreach (var supportProduct in supportProducts) { var supportUnit = XrmContext.CreateQuery("uomschedule").FirstOrDefault(unit => unit.GetAttributeValue <Guid>("uomscheduleid") == (supportProduct.GetAttributeValue <EntityReference>("defaultuomscheduleid") == null ? Guid.Empty : supportProduct.GetAttributeValue <EntityReference>("defaultuomscheduleid").Id)); var baseUom = XrmContext.CreateQuery("uom").FirstOrDefault(baseuom => baseuom.GetAttributeValue <string>("name") == supportUnit.GetAttributeValue <string>("baseuomname")); var uoms = XrmContext.CreateQuery("uom").Where(uom => uom.GetAttributeValue <Guid>("baseuom") == baseUom.Id); foreach (var u in uoms) { var amount = new Money(0); var priceListItem = XrmContext.GetPriceListItemByPriceListNameAndUom(supportProduct.GetAttributeValue <Guid>("productid"), u.Id, PriceListName); if (priceListItem != null) { amount = priceListItem.GetAttributeValue <Money>("amount"); } PlanPackageList.Items.Add(new ListItem( string.Format("{0} - {1} - {2}", supportProduct.GetAttributeValue <string>("name"), u.GetAttributeValue <string>("name"), amount.Value.ToString("c0")), string.Format("{0}&{1}", supportProduct.GetAttributeValue <Guid>("productid"), u.GetAttributeValue <Guid>("uomid")) )); } } }