locked
Calculation of Money/Currency Field RRS feed

  • Question

  • I need to fetch all related quote products under the quote entity. I used a query expression for this to retrieve the corresponding Total amount for each product. Now I need to sum the total amounts. My code is not working, the field I’m retrieving is a money/currency field.

     

    My code goes like this:

     

                            Guid RecordGuid = entity.GetAttributeValue<Guid>("quoteid");

     

                            QueryExpression query = new QueryExpression("quotedetail");

                            query.ColumnSet = new ColumnSet(new string[] { "nexus_marginalamount", "baseamount"});

                            query.Criteria.AddCondition("quoteid", ConditionOperator.Equal, RecordGuid);

                            query.PageInfo.ReturnTotalRecordCount = true;

     

                            EntityCollection result = service.RetrieveMultiple(query);

                            

                            Money  m = new Money ();

     

                            foreach (Entity EntityResult in result.Entities)

                            {

     

                               m = m + EntityResult.GetAttributeValue<Money>("nexus_marginalamount");

     

                            }

     

                            entity["nexus_marginalamount"] = new Money(m.Value);

    Thursday, September 12, 2013 10:03 AM

Answers

  • Hi,

    Something is Null because of which error is being thrown.

    Please make sure that objects like service is not null.  Try to debug the code. You can also put tracing through itracingservice.

    Thanks!

    • Marked as answer by khatski Friday, September 13, 2013 1:47 AM
    Thursday, September 12, 2013 10:38 AM

All replies

  • try with this line

    m.Value += EntityResult.GetAttributeValue<Money>("nexus_marginalamount").Value;

    instead of

    m = m + EntityResult.GetAttributeValue<Money>("nexus_marginalamount");


    My blog: www.crmanswers.net


    Thursday, September 12, 2013 10:12 AM
  • Hi,

    Try below code.

    decimal m=0;  

    foreach (Entity EntityResult in result.Entities)

                            {


           // Declare variable m
                              m = m +((Money)EntityResult.Attributes["nexus_marginalamount"]).Value

                            }

    entity["nexus_marginalamount"] = new Money(m);

    You can use the Fetchexpression and Aggregate functions for such calculation.

    Thanks!



    • Edited by Kalim Khan Thursday, September 12, 2013 10:21 AM
    Thursday, September 12, 2013 10:16 AM
  • Hi. Thanks! But I'm getting an error with this log

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: (Exception) QuotesPlug-inObject reference not set to an instance of an object.

       at NexusPluginLibrary.Quotes.Execute(IServiceProvider serviceProvider)

    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>

      </ErrorDetails>

      <Message>(Exception) QuotesPlug-inObject reference not set to an instance of an object.

       at NexusPluginLibrary.Quotes.Execute(IServiceProvider serviceProvider)

    </Message>

      <Timestamp>2013-09-12T10:14:59.1301955Z</Timestamp>

      <InnerFault i:nil="true" />

      <TraceText>

     

    [NexusPluginLibrary: NexusPluginLibrary.Quotes]

    [6561b262-1a16-e311-8bd5-36d7c49cdbe8: NexusPluginLibrary.Quotes: Update of quote]

     

     

    </TraceText>

    </OrganizationServiceFault>

    Thursday, September 12, 2013 10:16 AM
  • Thanks fo the support but i Got no luck with the codes, still not working.. 
    Thursday, September 12, 2013 10:22 AM
  • Hi,

    Something is Null because of which error is being thrown.

    Please make sure that objects like service is not null.  Try to debug the code. You can also put tracing through itracingservice.

    Thanks!

    • Marked as answer by khatski Friday, September 13, 2013 1:47 AM
    Thursday, September 12, 2013 10:38 AM
  • what do you mean sir? the field values that im retrieving might be null? 
    Thursday, September 12, 2013 10:45 AM
  • Hi,

    I was now able to make the code work... there was a null value at one of the quote product.. thanks for helping me out on this.. :)

    Thursday, September 12, 2013 10:47 AM
  • Hi,

    If it really helped you, Would you please mark this as helpful and Post as answer so that others will get the help on this?

    Thanks!

    Thursday, September 12, 2013 11:16 AM