简体   繁体   中英

C# Plugin For CRM2013 - Not receiving Product Name + Quantity in fetch xml query

I'm trying to retrieve data from a Grid in my Entity Orders. I've used advance find and selected the entity order products to get the product name and quantity that has been selected by the user in the grid

My code is as follows

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

            string fetchxml = @"
            <fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>
                <entity name='salesorderdetail'>
                    <attribute name='productid'/>
                    <attribute name='productdescription'/>
                    <attribute name='priceperunit'/>
                    <attribute name='quantity'/>
                    <attribute name='extendedamount'/>
                    <attribute name='salesorderdetailid'/>
                    <order descending='false' attribute='productid'/>
                        <link-entity name='salesorder' alias='au' to='salesorderid' from='salesorderid'>
                        <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>";

            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());
                            _product.Add(_entity.Attributes["quantity"].ToString());
                        }

                        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 will output a CSV file of the results, but I keep getting the error 'The key was not present in the dictionary'

Which I think the problem is to do with the following

'      _product.Add(_entity.Attributes["name"].ToString());
       _product.Add(_entity.Attributes["quantity"].ToString());
'

I've checked to ensure that the names I've inputted are correct in the Entity orderproduct

I'm unsure if it is a XMLQuery problem or something else

I have done another post which is similar to this so please do not mark down, I've made a new one as it is now explaining the whole problem and not just asking about XML Query

Thanks, Shaun

Update;

       _product.Add(_entity.Attributes["name"].ToString());
       _product.Add(_entity.Attributes["quantity"].ToString());

Has now been changed to

       _product.Add(_entity.Attributes["productid"].ToString());
       _product.Add(_entity.Attributes["quantity"].ToString());

This has stopped an error message appearing But not in the CSV folder instead of the name being there it displays

"Microsoft.Xrm.Sdk.EntityReference"

Update

Problem may be due to productid being a Lookup

       _product.Add(_entity.Attributes["productid"].ToString());
       _product.Add(_entity.Attributes["quantity"].ToString());

Is now as follows;

       _product.Add(_entity.Attributes["productid"].ToString());
       var actualName = ((Microsoft.Xrm.Sdk.EntityReference)_entity.Attributes["productid"]).Name;
       _product.Add(_entity.Attributes["quantity"].ToString());

When I threw a invalidpluginexecutionexeption(actualName) the name of the product is displayed But when the CSV file is outputted its still displaying

"Microsoft.Xrm.Sdk.EntityReference"

Update New code with suggested amendments;

string fetchxml = @"
            <fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>
                <entity name='salesorderdetail'>
                    <attribute name='productid'/>
                    <attribute name='productdescription'/>
                    <attribute name='priceperunit'/>
                    <attribute name='quantity'/>
                    <attribute name='extendedamount'/>
                    <attribute name='salesorderdetailid'/>
                    <order descending='false' attribute='productid'/>
                        <link-entity name='product' alias='ac' to='productid' from='productid'>
                            <attribute name ='name'/>
                        </link-entity>
                        <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>";

            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());

                           _product.Add(_entity.Attributes["quantity"].ToString());                          
                        }
                        CSVFile = string.Join(",", _product);

                        string AddressPath = "FM-OR" + "_";
                        string AddressSavePath = @"\\fm\CRMdata\maesteg\" + AddressPath + ".csv";
                        System.IO.File.WriteAllText(AddressSavePath, CSVFile.ToString());
                    }

                }

Error message appears of given key not present in dictionary

Update - Problem Solved

The problem was in fact the line

_product.Add(_entity.Attributes["name"].ToString());

The solution is as follwos

 _product.Add(((EntityReference)c.Attributes["productid"]).Name.ToString());

You would only need productid if for some reason you couldn't get product.

You could retrieve product name using something similar to:-

var productRecord = service.Retrieve("product", ((Microsoft.Xrm.Sdk.EntityReference)_entity.Attributes["productid"]).Id, new ColumnSet(new[] {"name"}));

However as you can get to product then the following should be suitable:-

<fetch distinct="false" mapping="logical" output-format="xml-platform" version="1.0">
    <entity name="salesorderdetail">
        <attribute name="productid"/>
        <attribute name="productdescription"/>
        <attribute name="priceperunit"/>
        <attribute name="quantity"/>
        <attribute name="extendedamount"/>
        <attribute name="salesorderdetailid"/>
        <order descending="false" attribute="productid"/>
        <link-entity name="product" alias="ab" to="productid" from="productid">
            <attribute name="name"/>
        </link-entity>
    </entity>
</fetch>

The ((Microsoft.Xrm.Sdk.EntityReference)_entity.Attributes["productid"]).Name; call is returning the name of that object (which is Microsoft.Xrm.Sdk.EntityReference) it doesn't know you are looking for the name of the product (which is its own attribute)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM