Sum the total Opportunity revenue and store in a field, how do I do this? RRS feed

  • Question

  • I'm looking for sample C# code to give me the total sum of the estimated revenue from all the Opportunity records, then I want to take that value and store it in a field called "Estimated Revenue" in an entity called "Balances". 

    I've looked at the sample provided in SDK but I've spent a week trying to work around. I just started Plugin dev 2 weeks ago. Would appreciate if anyone can point me in the right direction.

    Wednesday, November 20, 2013 10:38 AM

All replies

  • Hello,

    Could you provide information how opportunity is related to your custom entity or not related at all, at . you can write a simple plugin on opportunity update and check if estimated revenue is changed and get all the opportunities in system and calculate their total to your custom entity.

    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, November 20, 2013 4:47 PM
  • Hi Sanjeeb,

    Would you be better off putting the Estimated Revenue on the Account record and total up all the Opportunities related to that Account?

    Friday, November 29, 2013 1:10 PM
  • Hi Steve, the reason we have the Balances entity is so that we can see a rollup of ALL the estimated revenue from all opportunities and not just from one account. 

    Do you have any idea how this can be done? I think I might have searched the entire Internet on how to get this to work using the SDK.

    Friday, November 29, 2013 1:26 PM
  • // Fetch the sum of estimatedvalue for all opportunities.

    string estimatedvalue_avg = @" <fetch distinct='false' mapping='logical' aggregate='true'> <entity name='opportunity'> <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum' /> </entity> </fetch>";

    EntityCollection estimatedvalue_sum_result = _serviceProxy.RetrieveMultiple(new FetchExpression(estimatedvalue_sum));

    foreach (var c in estimatedvalue_sum_result.Entities) { decimal oppEstimatedSum = ((Money)((AliasedValue)c["estimatedvalue_sum"]).Value).Value; //System.Console.WriteLine("Sum of estimated value: " + oppEstimatedSum); }

    Friday, November 29, 2013 2:10 PM