Asked by:
CRM 2013 - FetchXML Query Javascript

Question
-
I have a custom entity called Issue which has a grid linked to a entity Cost To Business, users can add records from Cost To Business to the issue record. I want a overall cost to business field to be auto populated from the items in the grid. I've used a plugin to do this for me. Within the plugin I have constructed the following FetchXML Query to retrieve my data
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'/> <link-entity name='new_costtobusiness' alias='ab' to='new_issueid' from='new_issue_costid'> <attribute name='new_costtobusiness'/> </link-entity> </entity> </fetch>"; EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml)); { if (result != null && result.Entities.Count > 0) { List<string> _product = new List<string>(); foreach (Entity _entity in result.Entities)//loop through every record { costToBusiness = ((AliasedValue)_entity.Attributes["ab.new_costtobusiness"]).Value.ToString(); } throw new InvalidPluginExecutionException(costToBusiness); } }
However, when I'm calling the Invalid Plugin Execution Exception to view what the query is returning the variable "costToBusiness" is only holding "Microsoft.Xrm.Sdk.Money" and not the actual value which is in the record.
Does anyone know what I've done wrong?
S.Harrison
Monday, July 28, 2014 1:44 PM
All replies
-
How about something like this?
string query = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='account'> <attribute name='accountid' /> <attribute name='creditlimit' /> <order attribute='creditlimit' descending='false' /> </entity> </fetch>"; EntityCollection results = _serviceProxy.RetrieveMultiple(new FetchExpression(query)); decimal total = 0; foreach (Entity e in results.Entities) { if (e.Contains("creditlimit")) total += e.GetAttributeValue<Money>("creditlimit").Value; }
Jason Lattimer
My Blog - Follow me on Twitter - LinkedIn- Proposed as answer by JLattimerMVP, Moderator Monday, July 28, 2014 2:00 PM
Monday, July 28, 2014 2:00 PMModerator -
Hi JLattimer
I've tried your suggested way
When I call the Invalid Plugin Execution Exception now it displays
Exception of type 'Microsoft.Xrm.Sdk.Invalid Plugin Execution Exception' was thrown
I'm unsure now if your way has worked or not
Thanks, Shaun
S.Harrison
Monday, July 28, 2014 2:19 PM -
Instead of testing this through a plugin I'd suggest testing through a console application so you can more easily debug. There a few pre-built ones in the SDK you could use as a bases to get going quickly - I used the SOAPLogger project under 'samplecode\cs\client\soaplogger' to test this.
Jason Lattimer
My Blog - Follow me on Twitter - LinkedInMonday, July 28, 2014 3:08 PMModerator