web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to Handle currency conversion while updating a Money field using FetchXml Aggregate

(0) ShareShare
ReportReport
Posted on by

I am trying to Update Quote Total from the Sum of Quote detail Prices using FetchXml Aggregate.

Suppose my Quote's Transaction currency is EURO and Organization Base currency is USD.

I noticed that Fetxchml aggregate is always returning the total in USD (standard behavior) or personal preference. So if I try to update the Quote Total(EURO) its always ending up in wrong value. But is there a way to return the total in EURO and update it back in EURO so as to ensure its always correct. Below is my code:

 

Entity parent = (Entity)Context.PreEntityImages["PreImage"];

decimal Total = FetchResult(parent.Id, Service);

Entity quote = new Entity("quote");

quote.Id = parent.Id;

quote["fpig_quotegrossprofit"] = new Money (Total);

Service.Update(quote);

 

private static decimal FetchResult(Guid parent, IOrganizationService service)

       {

             string quoteref = parent.ToString();

 

               string value_sum = string.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'>

<entity name='quotedetail'>

<attribute name='fpig_grossprofit' alias='totalamount_sum' aggregate='sum'/>

<filter type='and'>

     <condition attribute='quoteid' operator='eq' uitype='quote' value = '{0}' />

   </filter>

</entity>

</fetch>", quoteref);

               decimal aggregate2 = 0;

 EntityCollection value_sum_result = (EntityCollection)service.RetrieveMultiple(new FetchExpression(value_sum));

 

               foreach (var c in value_sum_result.Entities)

               {

                   AliasedValue a = (AliasedValue)c["totalamount_sum"];

 

                   if (a.Value != null)

                   {

                       aggregate2 = ((Money)a.Value).Value;

                        }

 

               }

             return aggregate2;

          

       }

   }

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Yadnyesh Kuvalekar Profile Picture
    4,102 on at

    Hi Sameera,

    Which CRM version you are using? If it is above 2015 update 1, then roll up field is the ideal solution to your requirement.

    technet.microsoft.com/.../dn832162.aspx

    Thanks & Regards,

    Yadnyesh Kuvalekar

    Tick verify answer if this resolves your problem.

  • Yadnyesh Kuvalekar Profile Picture
    4,102 on at

    Is your issue resolved?

  • Verified answer
    Community Member Profile Picture
    on at

    Thanks for your reply

     

    Roll up fields wont help us as the field should have the latest value all the time, as its used for validation.

    I resolved this issue by changing the type of the quotedetail field (grossprofit) from Money to decimal.

    Now the Fetchxml will always return the aggregate value same as in transaction currency

  • Suggested answer
    Yadnyesh Kuvalekar Profile Picture
    4,102 on at

    Okay. How roll up field won't help you as you wanted to sum up currency amount from quote detail record to its parent records. Hence Roll up was the ideal solution. However if you always wanted to have latest value, then instead of writing plugin for aggregating the amount, you should write a plugin to calculate roll-up value manually.

    Below is the MSDN link:

    [View:https://msdn.microsoft.com/en-in/library/dn817863.aspx:750:50]

    on-demand-rollup.png

    Also, gross profit is currency field in real world, and hence setting its type to decimal is not ideal solution.

  • Community Member Profile Picture
    on at

    the field at Quote level is still a currency field. the one at the quote detail is not significant for us at the moment, and is used for only calculation purposes -that's why  I changed to decimal. However if we need to change that back to money, I will look at your suggestion.

  • Yadnyesh Kuvalekar Profile Picture
    4,102 on at

    Thanks. If you found my answers relevant and proper, feel free to tick verify answer.

    Thanks & Regards,

    Yadnyesh Kuvalekar

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans