简体   繁体   中英

Fetch XML Query Issue

Within the company I'm working for I've created a new entity in Microsoft Dynamics CRM 2013 called Issue, within the entity Issue there is a sub grid where users can add records from the entity Cost To Business.

The purpose is that users add the records when needed. I'm now using a FetchXML Query with a C# Plugin so that I can retrieve the Cost Attribute that is related to each record in the subgrid so that I can populate a Total Cost Field.

So far I have the following Query

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

string fetchxml =
    @"<fetch distinct='true' mapping='logical' output-format='xml-platform' version='1.0'>
          <entity name='new_issue'>
              <attribute name='new_issueid'/>
              <attribute name='new_name'/>
              <attribute name='createdon'/>
              <order descending='false' attribute='new_issueid'/>
              <filter type='and'> 
                  <condition attribute='new_issueid' operator='eq' value='" + orderID + @"'/> 
              </filter> 
              <link-entity name = 'new_costtype' alias='ac' to='new_issueid' from='new_costtypeissueid'>
                  <link-entity name='new_costtobusiness' alias='ad' to='new_costtobusinesscosttypeid' from='new_costtobusinessid'>
                       <attribute name='new_coststring'/>                                             
                  </link-entity>
              </link-entity>
          </entity>
      </fetch>"; 

However the Query can't be returning anything as the following code is not getting executed.

EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml));

if (result != null && result.Entities.Count > 0)
{
    List<int> _product = new List<int>();

    foreach (Entity _entity in result.Entities)//loop through every record
    {
        costToBusiness = ((AliasedValue)_entity.Attributes["ad.new_coststring"]).Value.ToString();
        total = (total) + Convert.ToInt32(costToBusiness);           
    }

    if(entity.Attributes.Contains("new_businessstring"))
    {
        entity.Attributes["new_businessstring"] = currencyName +  total.ToString();
        costToBusiness = "";
        total = 0;
        currencyName = "";
    }
    else
    {
        entity.Attributes.Add("new_businessstring", currencyName +  total.ToString());
        costToBusiness = "";
        total = 0;
        currencyName = "";            
    }
}

Does anyone have a reason/suggestion as to why this is happening? Thanks in advance

Update

Details From Downloaded Log from throw new InvalidPluginExecutionExeption

Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: <fetch distinct='true' mapping='logical' output-format='xml-platform' version='1.0'>
                    <entity name='new_issue'>
                        <attribute name='new_issueid'/>
                        <attribute name='new_name'/>
                        <attribute name='createdon'/>
                            <order descending='false' attribute='new_issueid'/>
                            <filter type='and'> 
                                <condition attribute='new_issueid' operator='eq' value='ddd33910-7c12-e411-a5f0-00155d550f0a'/> 
                            </filter> 
                            <link-entity name = 'new_costtype' alias='ac' to='new_issueid' from='new_costtypeissueid'>
                            <link-entity name='new_costtobusiness' alias='ad' to='new_costtobusinesscosttypeid' from='new_costtobusinessid'>
                                <attribute name='new_coststring'/>                                             
                            </link-entity>
                            </link-entity>
                    </entity>
              </fetch>Detail: 
<OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
  <ErrorCode>-2147220891</ErrorCode>
  <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
<KeyValuePairOfstringanyType>
  <d2p1:key>OperationStatus</d2p1:key>
  <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
</KeyValuePairOfstringanyType>
<KeyValuePairOfstringanyType>
  <d2p1:key>SubErrorCode</d2p1:key>
  <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">
2146233088</d2p1:value>
</KeyValuePairOfstringanyType>

`fetch distinct='true' mapping='logical' output-format='xml-platform'version='1.0'&gt;
                    &lt;entity name='new_issue'&gt;
                        &lt;attribute name='new_issueid'/&gt;
                        &lt;attribute name='new_name'/&gt;
                        &lt;attribute name='createdon'/&gt;
                            &lt;order descending='false' attribute='new_issueid'/&gt;
                            &lt;filter type='and'&gt; 
                                &lt;condition attribute='new_issueid' operator='eq' value='ddd33910-7c12-e411-a5f0-00155d550f0a'/&gt; 
                            &lt;/filter&gt; 
                            &lt;link-entity name = 'new_costtype' alias='ac' to='new_issueid' from='new_costtypeissueid'&gt;
                            &lt;link-entity name='new_costtobusiness' alias='ad' to='new_costtobusinesscosttypeid' from='new_costtobusinessid'&gt;
                                &lt;attribute name='new_coststring'/&gt;                                             
                            &lt;/link-entity&gt;
                            &lt;/link-entity&gt;
                    &lt;/entity&gt;
              &lt;/fetch&gt;</Message>
<Timestamp>2014-08-13T13:10:53.9593098Z</Timestamp>

[CostToBusiness: CostToBusiness.CostBusiness]
[2db9e3ba-3616-e411-a5f0-00155d550f0a: CostToBusiness.CostBusiness: Update of new_issue]


</TraceText>
</OrganizationServiceFault>

`

Sorry about the last part of the message, cannot for the life of me get it all into code format for some reason

If I understand correctly you are having new_issue as a child entity of some other entity with field to store aggregated cost from new_issue. In this case you should have PostOperation plug in (Create/Update/StateChange) on new_issue and your fetch xml should be aggregate fetch xml which will search for all the new_issue where lookup field id equal to parent entity ID and then aggregate the cost. In case of Delete you have to handle it in slightly different way. HTH

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