0

I have a Grid on my entity page Orders, users can add products to the entity grid

For my plugin I need to know what products have been inserted into the grid so that I can then produce a CSV file

I've tried using a FetchXML query to retrieve the data which is as follows;

string fetchxml = @"
<fetch mapping= 'logical'>
   <entity name ='product'>
      <attribute name = 'name'/>
   </entity>
 </fetch> ";

EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml));foreach(var c in result.Entities){
 if (result != null && result.Entities.Count > 0)
   {
     List<string> _product = new List<string>();
     foreach (Entity _entity in result.Entities)
       {
         _product.Add(_entity.Attributes["name"].ToString());
       }
string CSVFile = string.Join(",", _product.ToArray());string AddressPath = "FM-OR" + "_";
string AddressSavePath = @"\\fm\CRMdata\maesteg\" + AddressPath + ".csv";
System.IO.File.WriteAllText(AddressSavePath, CSVFile.ToString());
}
}

The code does produce the required CSV file that I need however it's selected every record in the entity products instead of the required ones in the grid. Any suggestions on how I sort this problem?

Thanks, Shaun

Sjharrison
  • 729
  • 3
  • 16
  • 39

2 Answers2

0

You need to use Link-Entity and Filter in your fetch query. Check the examples below:

Sample: Use aggregation in FetchXML

Fetch Xml should be something like this:

Guid orderId = (Guid)((Entity)context.InputParameters["Target"]).Id;

<?xml version="1.0"?>
    <fetch distinct="true" mapping="logical" output-format="xml-platform" version="1.0">
    <entity name="product">
        <attribute name="name"/>
        <attribute name="productnumber"/>
        <attribute name="subjectid"/>
        <attribute name="statecode"/>
        <attribute name="productid"/>
        <order descending="false" attribute="productnumber"/>
        <link-entity name="salesorderdetail" alias="aa" to="productid" from="productid">
            <link-entity name="salesorder" alias="ab" to="salesorderid" from="salesorderid">
                <filter type="and">
                    <condition attribute="salesorderid" operator="eq" value=orderId />
                </filter>
            </link-entity>
       </link-entity>
    </entity>
</fetch>
Scorpion
  • 4,495
  • 7
  • 39
  • 60
  • Hi thanks for your reply, "The SomeValue" part is what's been troubling me when I've been trying to add a filter in my query, for what I've tried I can't see a way of getting the value into the query, Thanks – Sjharrison Jul 01 '14 at 13:03
  • How do you call your plugin? – Nicknow Jul 01 '14 at 13:07
  • Its being called on the update of order entity. Post-operation and synchronous – Sjharrison Jul 01 '14 at 13:15
  • Hi, Next problem I have is that I need to retrieve the quantity required. The attribute is in the entity Order Product which name is salesorderdetail which has been used, i've tried creating a link entity as follows But have had no luck as error stating that the field doesn't exist in microsoft dynamics CRM – Sjharrison Jul 02 '14 at 09:45
  • Easiest option is to use `Advance Find` on CRM UI and if you are happy with the results you got then download the `Fetch XML` from UI. Once you have system generated `Fetch XML` then its easy to make required changes to it. – Scorpion Jul 02 '14 at 09:52
  • I have changed the XML around, now my XML will get me just the Quantity of the products, unsure how to get it to do both at the same time which is very frustrating, code is as follows on next comment – Sjharrison Jul 02 '14 at 10:49
0

"To some value" is the id of the entity where the plugin is triggered from check this link: http://msdn.microsoft.com/en-us/library/gg309673.aspx

your code should look like:

@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
 <entity name='product'>
     <attribute name = 'name'/>
     <order attribute='name' descending='false' />
     <filter type='and'>
         <condition attribute='productid' operator='eq' value=" + context.InputParameters["Target"].id + @" />
     </filter>
     <link-entity name='order' from='projectid' to='project' alias='Order'>
       ........
     </link-entity>
 </entity>
</fetch>"

bear in mind that probably the string concatenation that i did is wrong, but i had to do it like that to highlight the syntax :)

Mauro De Biasio
  • 1,146
  • 6
  • 16
  • Hi, thanks for your help, the line context.InputParmeters["Target"].id Threw a error saying that there is no method for "id".... Is that to do with the string concatenation problem that you mentioned or is it something im missing? Thanks – Sjharrison Jul 02 '14 at 08:24
  • Uhm is something that you are missing :D it can be .Id instead of id – Mauro De Biasio Jul 03 '14 at 05:40