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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

plugin to caluculate anual revenue

(0) ShareShare
ReportReport
Posted on by

how to write a plugin to display annual revenue

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Waqar Sohail Profile Picture
    on at

    Where you want to display? Plugin is use as a trigger on following Entity actions, Like Create, Update, Delete, Status Change etc.

    For displaying annual revenue you have to create report.

  • Community Member Profile Picture
    on at

    when ever field is updated

  • Aileen Gusni Profile Picture
    44,524 on at

    Hi Avinay,

    What is 'display annual revenue' referring to?

    Opportunity revenue or what revenue?

    Or field in the Account?

  • Community Member Profile Picture
    on at

    oppurtunity revenue

  • Suggested answer
    Waqar Sohail Profile Picture
    on at

    I think you can create a field for the annual revenue and on update of Opportunity you can update that field with add of new values.

    also look at this workflow for the idea.

    blog.clickdimensions.com/.../using-crm-workflow-to-calculate-weighted-revenue-on-opportunity-records.html

  • Aileen Gusni Profile Picture
    44,524 on at

    What is your planned calculation, based on what field?

    Because CRM by default has its revenue calculated from total product amount, but it is not annual, it is only one revenue.

    If you need annual revenue of the total Opportunity you can just create chart or report, standard CRM chart can group by year as well and you put into dashboard or you can create Goal to track actual vs in progress.

  • Community Member Profile Picture
    on at

    hi aileen consider opportunity have sub parts like opp1 , opp2 , opp3

    suppose if annual revenue is updated in opp3 means the sum of all the opportunities should be displayed in Main oppurtunity , i need code to display the sum of oppurtunity

  • Royal King Profile Picture
    27,686 on at

    Create workflow on update of the annual revenue field in the child opportunities and in the workflow increment or decrement the annual revenue in the parent opportunity annual revenue field.  You may need another field in the opportunity to keep old annual revenue value. if you ave old and update annual revenue in the child opportunity then you can get difference between them and based on the difference either you have to increment or decrement annual revenue in the parent opportunity.

  • Aileen Gusni Profile Picture
    44,524 on at

    Hi Avinay,

    You want to store or only display the value? because there is code to get the value from subgrid total, but I guess if you need to store, then here are the steps.

    You can use this link to your code.

    dynamicsofdynamicscrm.wordpress.com/.../aggregate-fetchxml-queries-for-dynamics-crm-20112013

    Steps:

    1. have you created the relationship self referential relationship Opportunity to Opportunity?

    2. If you are using default revenue field in Opportunity, you need to set it to User Provided not System Calculated

    www.pedroinnecco.com/.../opportunity-entity-2013

    3. But, I would love to recommend you to use new field, create a new revenue field then make the default field to System Calculated (if you agree that revenue of the child is based on total product amount)

    4. Create a plugin when Opportunity is created or updated (can filter attribute registered and run/executed only if revenue is changed)

    5. The code I guess will be similar like this:

    *I use early Bound class

    if (TargetEntity.EstimatedValue!= null) //estimatedvalue of the child updated or created

                       {

                           //the logic is you have to find the Opportunity parent

                           Opportunity parentOpportunity = new Opportunity();

                           parentOpportunity = null;

                           if(TargetEntity.new_parentopportunityId != null)

                             parentOpportunity = TargetEntity.new_parentopportunityId;

                           else

                           {

                               if(preImageEntity.parentopportunityId  != null) //you need to define preimage first

                                    parentOpportunity = preImageEntity.new_parentopportunityId;

                           }

                           if(parentOpportunity !=null)

                           {

                                   parentOpportunity["new_totalchildrevenue"] = new Money(TargetEntity.EstimatedValue);

                                   service.Update(parentOpportunity);

                           }

                       }

    //update the parent field it will be triggered from above transaction

    if (TargetEntity.new_totalchildrevenue!= null) //triggered by new_totalchildrevenue //updated through Opportunity if Contains any update of Annual revenue

                               {

                                   string strFetchXML = string.Empty;

                                   strFetchXML = @"

                                   <fetch distinct='false' mapping='logical' aggregate='true'>

                                       <entity name='opportunity'>

                                           <attribute name='tfp_totalinternaldeduction' alias='tfp_amount_sum' aggregate='sum' />

                                           <filter type='and'>

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

                                           </filter>

                                       </entity>

                                   </fetch>";

                                   strFetchXML = string.Format(strFetchXML, TargetEntity.Id);

                                   TargetEntity.tfp_TotalInternalDeduction = new Money(Utility.AggregateFetchXMLMoney(service, strFetchXML, "tfp_amount_sum"));

                               }

    This is the referenced function

    public static decimal AggregateFetchXMLMoney(IOrganizationService service, string strXMLParam, string strAggregateAlias)

           {

               string strXML = strXMLParam;

               //string estimatedvalue_sum = @"

               //<fetch distinct='false' mapping='logical' aggregate='true'>

               //    <entity name='opportunity'>

               //       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum' />

               //    </entity>

               //</fetch>";

               EntityCollection aggregateResult = service.RetrieveMultiple(new FetchExpression(strXML));

               decimal totalValue = 0;

               foreach (var c in aggregateResult.Entities)

               {

                   decimal aggregate2 = 0;

                   if (c.Attributes.Contains(strAggregateAlias))

                   {

                       aggregate2 = ((Money)((AliasedValue)c[strAggregateAlias]).Value).Value;

                       totalValue = aggregate2;

                   }

               }

               return totalValue;

           }

    Register during postCreate, postUpdate (with preimage), and postDelete

    for postDelete, just define value for update as 0.

    I am not sure it will work on your scenario, but I have similar situation before you might need to adjust the code modify it based on your entity and scenario and I am not using editor right now, then you can modify based on your field name and maybe I have some typos. I just help you to share some ideas and find out the method if you keen to use plugin.

    But I guess you need to be careful since you will register in the same entity, that is Opportunity, the plugin can be executed infinite if you don't put proper filter attribute or maybe you can use context.Depth function.

    Hope this helps!

    Thanks.

  • Aileen Gusni Profile Picture
    44,524 on at

    Hi Avinay,

    You want to store or only display the value? because there is code to get the value from subgrid total, but I guess if you need to store, then here are the steps.

    You can use this link to your code.

    dynamicsofdynamicscrm.wordpress.com/.../aggregate-fetchxml-queries-for-dynamics-crm-20112013

    Steps:

    1. have you created the relationship self referential relationship Opportunity to Opportunity?

    2. If you are using default revenue field in Opportunity, you need to set it to User Provided not System Calculated

    www.pedroinnecco.com/.../opportunity-entity-2013

    3. But, I would love to recommend you to use new field, create a new revenue field then make the default field to System Calculated (if you agree that revenue of the child is based on total product amount)

    4. Create a plugin when Opportunity is created or updated (can filter attribute registered and run/executed only if revenue is changed)

    5. The code I guess will be similar like this:

    *I use early Bound class

    if (TargetEntity.EstimatedValue!= null) //estimatedvalue of the child updated or created

                       {

                           //the logic is you have to find the Opportunity parent

                           Opportunity parentOpportunity = new Opportunity();

                           parentOpportunity = null;

                           if(TargetEntity.new_parentopportunityId != null)

                             parentOpportunity = TargetEntity.new_parentopportunityId;

                           else

                           {

                               if(preImageEntity.parentopportunityId  != null) //you need to define preimage first

                                    parentOpportunity = preImageEntity.new_parentopportunityId;

                           }

                           if(parentOpportunity !=null)

                           {

                                   parentOpportunity["new_totalchildrevenue"] = new Money(TargetEntity.EstimatedValue);

                                   service.Update(parentOpportunity);

                           }

                       }

    //update the parent field it will be triggered from above transaction

    if (TargetEntity.new_totalchildrevenue!= null) //triggered by new_totalchildrevenue //updated through Opportunity if Contains any update of Annual revenue

                               {

                                   string strFetchXML = string.Empty;

                                   strFetchXML = @"

                                   <fetch distinct='false' mapping='logical' aggregate='true'>

                                       <entity name='opportunity'>

                                           <attribute name='tfp_totalinternaldeduction' alias='tfp_amount_sum' aggregate='sum' />

                                           <filter type='and'>

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

                                           </filter>

                                       </entity>

                                   </fetch>";

                                   strFetchXML = string.Format(strFetchXML, TargetEntity.Id);

                                   TargetEntity.tfp_TotalInternalDeduction = new Money(Utility.AggregateFetchXMLMoney(service, strFetchXML, "tfp_amount_sum"));

                               }

    This is the referenced function

    public static decimal AggregateFetchXMLMoney(IOrganizationService service, string strXMLParam, string strAggregateAlias)

           {

               string strXML = strXMLParam;

               //string estimatedvalue_sum = @"

               //<fetch distinct='false' mapping='logical' aggregate='true'>

               //    <entity name='opportunity'>

               //       <attribute name='estimatedvalue' alias='estimatedvalue_sum' aggregate='sum' />

               //    </entity>

               //</fetch>";

               EntityCollection aggregateResult = service.RetrieveMultiple(new FetchExpression(strXML));

               decimal totalValue = 0;

               foreach (var c in aggregateResult.Entities)

               {

                   decimal aggregate2 = 0;

                   if (c.Attributes.Contains(strAggregateAlias))

                   {

                       aggregate2 = ((Money)((AliasedValue)c[strAggregateAlias]).Value).Value;

                       totalValue = aggregate2;

                   }

               }

               return totalValue;

           }

    Register during postCreate, postUpdate (with preimage), and postDelete

    for postDelete, just define value for update as 0.

    I am not sure it will work on your scenario, but I have similar situation before you might need to adjust the code modify it based on your entity and scenario and I am not using editor right now, then you can modify based on your field name and maybe I have some typos. I just help you to share some ideas and find out the method if you keen to use plugin.

    But I guess you need to be careful since you will register in the same entity, that is Opportunity, the plugin can be executed infinite if you don't put proper filter attribute or maybe you can use context.Depth function.

    Hope this helps!

    Thanks.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans