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 :

How to use FetchXML query for multiple dynamic conditions at runtime

Varun Singh Profile Picture Varun Singh 943
Hi All,
In the below example I have explained how at runtime you can append the Fetch XML Query. In this example, I am getting the material record list at runtime and based on the number of Material record in the list I am appending the Query condition.
public void GetMaterialPricePerUnit(List<Material> materials, Guid priceListId)
        {
            var query = new StringBuilder(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                            <entity name='new_EntityMaterail'>
                                               <attribute name='amount' />
                                               <attribute name='productid' />
                                               <filter type='and'>
                                                    <condition attribute='pricelevelid' operator='eq' value='" + priceListId + @"' />
                                                    <condition attribute='productid' operator='in'>");
            // add material's to filter
            materials.ForEach(material => query.Append("        <value>" + material.Id + "</value>"));
            query.Append(@"            </condition>
                                    </filter>
                                 </entity>
                               </fetch>");
            ;
            var request = new FetchExpression(query.ToString());
            var response = Service.RetrieveMultiple(request);
            response.Entities.ToList().ForEach(priceListitem =>
            {
                if (priceListitem.Contains("productid") && priceListitem.Contains("amount"))
                {
                    var material = materials.Where(x => x.Id.Equals(((EntityReference)priceListitem["productid"]).Id))
                                            .FirstOrDefault();
                    if (material != null)
                    {
                        material.PricePerUnit = (Money)priceListitem["amount"];
                    }
                }
            });
        }

Comments

*This post is locked for comments