This project is read-only.

FetchXML question

Sep 3, 2012 at 10:48 AM

Hello guys,

I have a fetchXML query like this:

        "<fetch mapping='logical' aggregate='true'>" +
	        "<entity name='quotedetail'>" +
		        "<attribute name='extendedamount' alias='sumExtendedAmount' aggregate='sum'/>" +
                "<filter type='and'>" +
			        "<condition attribute='quotedetailid' operator='in'>" +
				        "<value>" + stringIds + "</value>" +
			        "</condition>" +
		        "</filter>" +
	        "</entity>" +

which works great with XrmSvcToolkit.fetch, but it's returning the amount in the Base Currency when I parse it like this:  

quoteLinesTotalAmount = result.entities[0]['sumExtendedAmount'].Value;

This complicates things when the Currency of the record is different because I have to deal with the exchange rate etc.   I'd like to get the amount in the Currency of the record, not the Base Currency... Is it possible?


Nov 15, 2012 at 3:28 AM


The behaviour is by design. You're running aggregate over the currency field and CRM has no choice but to convert all values to base currency. How else would you deal with the situation where some quotes are in dollars, some in euros and others - in pounds?

If you retrieve a single record then you get back extended values that include information about transaction currency.


Nov 16, 2012 at 7:52 AM

Thanks George, that makes sense! 

All the best with everything,


Jun 19, 2013 at 4:06 PM
Hi Guys, I am trying to execute a very similar query to this but the execution just fails completely. I've tried executing the FetchXML standalone and I get the following error:

Unable to cast object of type 'System.String' to type 'Microsoft.Xrm.Sdk.Money'.

The query I'm using is here:

<fetch mapping='logical' aggregate='true'>
<entity name='new_entityid'>
<attribute name='new_fieldvalue' alias='Total' aggregate='sum'/>
<filter type='and'>
<condition attribute='new_entityid' operator='eq' value='GUID' />

Any help you can offer would be amazing as this is driving me mental.
Jun 19, 2013 at 4:36 PM
@UberMunchkin, the error message you posted is probably coming from your C# code. By looking at the FetchXML query, I don't see anything wrong, except that you might have a typo with your entity name.

BTW, this seems to be a whole new topic.
Jun 19, 2013 at 4:39 PM
Hmm, not using C#, I'm using Javascript.

It's very frustrating as there is no other useful information provided by the error. Thanks for responding though.
Jun 19, 2013 at 4:41 PM
@UberMunchkin, this is strange, can you run the same query if you use Stunnware Tools?
Jun 19, 2013 at 4:47 PM
Yes, it executes fine in the Stunnware tools.
Jun 19, 2013 at 6:08 PM
@UberMunchkin, that's very strange. JavaScript should never report a type of 'System.String' or 'Microsoft.Xrm.Sdk.Money'.
Jun 19, 2013 at 11:00 PM


Based on the error message I’d say error is definitely coming from the server-side. If I did not know better I’d say that the field new_fieldvalue is not money type.

This speculation aside, I would try to run Fiddler2, run query through Stunnware then through client code and pick up the differences in trace.



Jun 24, 2013 at 10:50 AM
Interestingly enough the error turned out to be naming.

The Fetch XML executed just fine in the Stunnware Tools but blew up every time I tried to run it using XrmSvcToolkit. So on a whim I changed this line:

<attribute name='new_fieldvalue' alias='Total' aggregate='sum'/>

to this:

<attribute name='new_fieldvalue' alias='new_fieldvalue_Total' aggregate='sum'/>

and it worked just fine. It looks like something in the XrmSvcToolkit or one of it's dependencies doesn't like you using the alias 'Total' in aggregate queries.

Cheers for the help folks.
Jun 24, 2013 at 12:30 PM

Hmmm, interesting… I think the problem could be related to the endpoints in use as Stunnware tools use CRM 4 endpoint, AFAIK. I don’t see any other reason why it would work differently.