public Fetch CreateFetch() { var columnSet = ColumnSets.SelectMany(set => set.ColumnSet).ToArray(); var fetch = new Fetch { Entity = new FetchEntity(LogicalName) { Attributes = columnSet.Select(column => new FetchAttribute(column)).ToList() }, SkipCache = true }; if (ActiveStateCode != null) { fetch.AddFilter(new Filter { Conditions = new List <Condition> { new Condition("statecode", ConditionOperator.Equal, ActiveStateCode.Value) } }); } return(fetch); }
public override bool TryApply(IViewConfiguration configuration, IDataAdapterDependencies dependencies, IDictionary <string, string> customParameters, Fetch fetch) { if (!IsApplicable(configuration)) { return(false); } // This should be the ID of the parent opportunity for the opportunity product. If it's // null, we can't do any filtering. if (configuration.ModalLookupFormReferenceEntityId == null) { return(false); } // Filter out non-published products, fetch.AddFilter(new Filter { Conditions = new List <Condition> { new Condition("statecode", ConditionOperator.Equal, 0) } }); // Use distinct because you can have multiple price list items per product in a price list. fetch.Distinct = true; // Add the primary ID attribute explicitly because that allows Entity.Id to be set properly // when doing a distinct query. fetch.Entity.AddAttributes("productid"); var productPriceLevelFilter = new Filter { Type = LogicalOperator.And, Conditions = new List <Condition> { new Condition( "pricelevelid", ConditionOperator.Equal, // If the opportunity doesn't have a price list, or we fail to get it for some reason, filter // by the empty GUID so that nothing will be returned. GetPriceListId(dependencies, configuration.ModalLookupFormReferenceEntityId.Value).GetValueOrDefault(Guid.Empty)) } }; // Filter by the selected unit of measure if available, string uomIdParameter; Guid uomId; if (customParameters != null && customParameters.TryGetValue("uomid", out uomIdParameter) && Guid.TryParse(uomIdParameter, out uomId)) { productPriceLevelFilter.Conditions.Add(new Condition("uomid", ConditionOperator.Equal, uomId)); } // Filter by price list items in parent opporunity price list. fetch.AddLink(new Link { Name = "productpricelevel", FromAttribute = "productid", ToAttribute = "productid", Type = JoinOperator.Inner, Filters = new List <Filter> { productPriceLevelFilter } }); return(true); }
/// <summary> /// Retrieves the list of Products available to the current user /// </summary> /// <returns>Product entity collection</returns> public List <Guid> GetProducts() { // If anonymous user, return nothing if (this.CurrentUserEntityReference == null) { return(Enumerable.Empty <Guid>().ToList()); } var productFetch = new Fetch { Distinct = true, Entity = new FetchEntity { Name = "product", Attributes = new List <FetchAttribute> { new FetchAttribute("productid") }, Filters = new List <Filter>() } }; var associatedToAccountOrContactFilter = new Filter { Type = LogicalOperator.Or, Conditions = new List <Condition>(), Filters = new List <Filter>() }; // Get alias generator instance to maintain alias names consistency // via postfix incrementation var linkEntityAliasGenerator = LinkEntityAliasGenerator.CreateInstance(); // Retrieve Contact to Product relationships and build Entity Permission links var contactToProductRelationshipNamesCollection = this.GetDelimitedSiteSettingValueCollection(ContactToProductRelationshipNames, ContactToProductFallbackRelationshipName); var contactLink = this.BuildLinksAndFilterChain( contactToProductRelationshipNamesCollection, productFetch, associatedToAccountOrContactFilter, this.CurrentUserEntityReference, null, OwningCustomerType.Contact, linkEntityAliasGenerator); productFetch.AddLink(contactLink); if (this.ParentCustomerEntityReference != null && this.ParentCustomerEntityReference.LogicalName == "contact") { // Retrieve parent Contact to Product relationships and build Entity Permission links var parentContactLink = this.BuildLinksAndFilterChain( contactToProductRelationshipNamesCollection, productFetch, associatedToAccountOrContactFilter, this.ParentCustomerEntityReference, null, OwningCustomerType.Contact, linkEntityAliasGenerator); productFetch.AddLink(parentContactLink); } else if (this.ParentCustomerEntityReference != null && this.ParentCustomerEntityReference.LogicalName == "account") { // Retrieve Account to Product relationships and build Entity Permission links var accountToProductRelationshipNamesCollection = this.GetDelimitedSiteSettingValueCollection(AccountToProductRelationshipNames, AccountToProductFallbackRelationshipName); var accountLink = this.BuildLinksAndFilterChain( accountToProductRelationshipNamesCollection, productFetch, associatedToAccountOrContactFilter, null, this.ParentCustomerEntityReference, OwningCustomerType.Account, linkEntityAliasGenerator); productFetch.AddLink(accountLink); } var accountOrContactNotNullFilter = new Filter { Type = LogicalOperator.Or, Conditions = associatedToAccountOrContactFilter.Conditions.Select( condition => new Condition { EntityName = condition.EntityName, Attribute = condition.Attribute, Operator = ConditionOperator.NotNull }).ToList() }; // This is the AND Filter that will ensure state is Active and the Product is joined to either Contact or Account productFetch.AddFilter(new Filter { Type = LogicalOperator.And, Conditions = new List <Condition> { new Condition("statecode", ConditionOperator.Equal, 0) }, Filters = new List <Filter> { accountOrContactNotNullFilter, associatedToAccountOrContactFilter, } }); var productsCollection = productFetch.Execute(this.Portal.ServiceContext as IOrganizationService); return(productsCollection.Entities.Select(x => x.Id).ToList()); }