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;
}
}