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)

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

I have the same question (0)
  • Suggested answer
    TheMarkChristie Profile Picture
    10,328 Moderator on at

    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

  • Suggested answer
    Community Member Profile Picture
    on at

    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();

                       }

                   }

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