Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Duration calculation of Opportunity stage before moving to another stage

(0) ShareShare
ReportReport
Posted on by 910

Hi ,

 

I have a requirement where I have to calculate overall duration of a particular opportunity stage ,i.e. How long the opportunity was in the same stage, before moving to another one.

 

I dont want to use any custom code, would like to achieve the same through out of box functionality.

 

 Thanks in Advance.

 

*This post is locked for comments

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Duration calculation of Opportunity stage before moving to another stage

    please modify the code

    function calculateAgeing() {

       //Function to calculate Ageing On change of BPF Stage

       try {

           if (Xrm.Page.context.client.getClient() == "Web" || Xrm.Page.context.client.getClient() == "Outlook") {

               if (Xrm.Page.getAttribute("statecode").getValue() == 0) {

                   var currentStage = Xrm.Page.data.process.getActiveStage();

                   var currStageCategory = currentStage.getCategory().getValue();

                   var dafaultStageCategory = defaultStageOnLoad.getCategory().getValue();

                   //Stage 1 Ageing

                   if ((dafaultStageCategory == 0 || dafaultStageCategory == 4) && (currStageCategory != dafaultStageCategory)) {

                       if (Xrm.Page.getAttribute("ac_stagemodifieddate").getValue() != null && Xrm.Page.getAttribute("ac_stagemodifieddate").getValue() != undefined) {

                           var s1ModifiedDate = Xrm.Page.getAttribute("ac_stagemodifieddate").getValue();

                           var s2Modifieddate = new Date();

                           var spanDiff = ((s2Modifieddate - s1ModifiedDate) / 1000) / 60;

                           Xrm.Page.getAttribute("ac_stagemodifieddate").setValue(s2Modifieddate);

                           if (Xrm.Page.getAttribute("ac_s1ageing").getValue() != null && Xrm.Page.getAttribute("ac_s1ageing").getValue() != undefined) {

                               Xrm.Page.getAttribute("ac_s1ageing").setValue(Xrm.Page.getAttribute("ac_s1ageing").getValue() + spanDiff);

                           }

                           else {

                               Xrm.Page.getAttribute("ac_s1ageing").setValue(spanDiff);

                           }

                           if (currStageCategory == 1 || currStageCategory == 5) {

                               Xrm.Page.getAttribute("ac_opportunitystage").setValue(948140001);

                           }

                           else if (currStageCategory == 2 || currStageCategory == 6) {

                               Xrm.Page.getAttribute("ac_opportunitystage").setValue(948140002);

                           }

                           else if (currStageCategory == 3 || currStageCategory == 7) {

                               Xrm.Page.getAttribute("ac_opportunitystage").setValue(948140003);

                               ///change sold margin on change of opportunity stage to stage 4 and proposal status should be accepted

                               var proposalStatus = Xrm.Page.getAttribute("ac_proposalstatus").getValue();

                               if (proposalStatus != null && proposalStatus != undefined && proposalStatus == 948140001) {

                                   var grossMargin = Xrm.Page.getAttribute("ac_grossmarginpercentage").getValue();

                                   if (grossMargin != null && grossMargin != undefined) {

                                       Xrm.Page.getAttribute("ac_soldmargin").setValue(grossMargin);

                                       Xrm.Page.getAttribute("ac_soldmargin").setSubmitMode("always");

                                   }

                               }

                               else {

                                   Xrm.Page.getAttribute("ac_soldmargin").setValue(null);

                                   Xrm.Page.getAttribute("ac_soldmargin").setSubmitMode("always");

                               }

                           }

                           Xrm.Page.data.entity.save();

                           defaultStageOnLoad = Xrm.Page.data.process.getActiveStage();

                       }

                       else {

                           var s1ModifiedDate = Xrm.Page.getAttribute("createdon").getValue();

                           var s2Modifieddate = new Date();

                           var spanDiff = ((s2Modifieddate - s1ModifiedDate) / 1000) / 60;

                           Xrm.Page.getAttribute("ac_stagemodifieddate").setValue(s2Modifieddate);

                           if (Xrm.Page.getAttribute("ac_s1ageing").getValue() != null && Xrm.Page.getAttribute("ac_s1ageing").getValue() != undefined) {

                               Xrm.Page.getAttribute("ac_s1ageing").setValue(Xrm.Page.getAttribute("ac_s1ageing").getValue() + spanDiff);

                           }

                           else {

                               Xrm.Page.getAttribute("ac_s1ageing").setValue(spanDiff);

                           }

                           if (currStageCategory == 1 || currStageCategory == 5) {

                               Xrm.Page.getAttribute("ac_opportunitystage").setValue(948140001);

                           }

                           else if (currStageCategory == 2 || currStageCategory == 6) {

                               Xrm.Page.getAttribute("ac_opportunitystage").setValue(948140002);

                           }

                           else if (currStageCategory == 3 || currStageCategory == 7) {

                               Xrm.Page.getAttribute("ac_opportunitystage").setValue(948140003);

                               ///change sold margin on change of opportunity stage to stage 4 and proposal status should be accepted

                               var proposalStatus = Xrm.Page.getAttribute("ac_proposalstatus").getValue();

                               if (proposalStatus != null && proposalStatus != undefined && proposalStatus == 948140001) {

                                   var grossMargin = Xrm.Page.getAttribute("ac_grossmarginpercentage").getValue();

                                   if (grossMargin != null && grossMargin != undefined) {

                                       Xrm.Page.getAttribute("ac_soldmargin").setValue(grossMargin);

                                       Xrm.Page.getAttribute("ac_soldmargin").setSubmitMode("always");

                                   }

                               }

                               else {

                                   Xrm.Page.getAttribute("ac_soldmargin").setValue(null);

                                   Xrm.Page.getAttribute("ac_soldmargin").setSubmitMode("always");

                               }

                           }

                           Xrm.Page.data.entity.save();

                           defaultStageOnLoad = Xrm.Page.data.process.getActiveStage();

                       }

                   }

  • Suggested answer
    TheMarkChristie Profile Picture
    10,328 Moderator on at
    RE: Duration calculation of Opportunity stage before moving to another stage

    Hi Alok

    You would need to create 3 fields

    Stage Start (date/Time) Stage End (Date/Time) Stage Time (Time Diff calculation)

    You will need to create a workflow that checks for the business process stage and if it equals the stage you want to time then you stamp Stage Start, Then do the same when it moves to the next stage and stamp Stage End - This will then calculate Stage Time Diff

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Featured topics

Product updates

Dynamics 365 release plans
Liquid error: parsing "/globalsearch/?q=(Error" - Not enough )'s.