function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tgk1tgk1 

Derive the number of days in a particular stage value

Hi everyone-

 

My organization has 8 values within the Stage field.  Analytic snapshots have proven unsuccessful for our particular organization in the past, so I'm looking to get creative.

 

The requirement here is to figure out how many days were spent in a particular stage (without getting overwritten).  I'm thinking about doing something where I create 8 fields (Stage 0 Age, Stage 1 Age, etc.) that can capture an integer value of how many days were spent in that particular stage.  This would allow us to really get down to the fine grain levels of reporting that our executive team is starting to require.  I believe something like this would have to be done with a workflow field update, as well as a formula field, but I'm not quite sure how I would do it.  Does anybody have any ideas?

MarrisMarris

Hi,

 

        May i know What object are you talking about Is it a custom object you are created with stages or the Standard object like opportunity which has stages of its own for calculating stages days

 

 

Thanks

Marris

tgk1tgk1

The opportunity object, and the standard "Stage" field within this object.

MarrisMarris

Hi 

 

  Just Create a Custom field called Days in opportunity to show no . of days between stages and follow this trigger.It Solves your problem

 

trigger DayCalculation on Opportunity (after update) {
     Public integer days;
      for(Opportunity opp:Trigger.new){
        Opportunity oldOpp = Trigger.oldMap.get(opp.ID);    
        Opportunity newOpp = Trigger.newMap.get(opp.ID);
        string oldStage = oldOpp.StageName;
        string newStage = newOpp.StageName;
            if(oldStage != newStage){        
             OpportunityHistory oppHis = [Select createdDate from OpportunityHistory where opportunityId=:opp.id and Stagename=:oldStage limit 1];               
             OpportunityHistory newoppHis =[Select createdDate from OpportunityHistory where opportunityId=:opp.id and Stagename=:newStage limit 1];
             Date timeInDate = oppHis.createdDate.date();
             Date timeOutDate = newoppHis.createdDate.date();
             days = timeInDate.daysBetween(timeOutDate);
             Opportunity objopp = [Select Days__c from Opportunity where id=: opp.id];
             objopp.Days__c = days;
             //objopp.Days__c = 10;
             update objopp;
       }
   }
        
       
}

 

If it solves your problem  mark it as solution or otherwise give me update about issues if any..

 

Thanks

Marris

 

 

tgk1tgk1

Thanks for your reply Marris.  However I don't believe that would solve my problem since the number of days value will keep being overwritten when a stage changes.  I need to be able to see stage level metrics (i.e. how many days specificially were in stage 3, 4, etc.).

MarrisMarris

Hi 

        Did u want it see for every individual opportunity or as a whole you want a report

     

You can do this thing Just create a button in the detail page of opportunity For that button click create  a new VF page and a controller class that handles no.of days like my trigger and show it in data table in that VF page for all stages

 

 

Thanks

Marris