0

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

Sjharrison
  • 729
  • 3
  • 16
  • 39
  • Need additional Context. How is the code supposed to be getting executed? Is it in a plugin? How is the plugin registered? – Daryl Aug 12 '14 at 17:31
  • Hi @Daryl, This is a plugin and it is registered on update, pre-operation and synchronous (pre operation as the plugin is updating a field) Thanks – Sjharrison Aug 13 '14 at 07:45
  • Are you expecting the plugin to fire when the user adds a new cost to business? – Daryl Aug 13 '14 at 11:00
  • No the plugin is expected to fire when the record is saved/updated – Sjharrison Aug 13 '14 at 11:22
  • The problem is to do with the query, as I put in a throw new InvalidPluginExecutionExeception in my foreach loop which didn't get called, therefore the Query must be returning nothing back – Sjharrison Aug 13 '14 at 11:36
  • I'd throw an exception as soon as you generate the FetchXml that spits out the FetchXml, just to be sure that you're generating the FetchXml you're expecting. – Daryl Aug 13 '14 at 12:37
  • okay so I added the following `throw new InvalidPluginExecutionException(fetchxml);` just after the and the error box that appeared was also blank – Sjharrison Aug 13 '14 at 12:43
  • Is there any Download Error details? – Daryl Aug 13 '14 at 13:07
  • I'll post downloaded log details as an update now – Sjharrison Aug 13 '14 at 13:14

1 Answers1

0

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

MaKeer
  • 61
  • 6
  • Cannot have it as PostOperation as I am using the plugin to update a field. Therefore, it has to be PreValidation/PreOperation – Sjharrison Aug 19 '14 at 08:12