7

What is the best way to get Opportunity Stage Change History using SOQL?

Basically, what I need is the same list as in Opportunity page in Salesforce, where list of stages and dates of modifications are listed.

2 Answers 2

11

You can use the OpportunityFieldHistory Object for this.

    List<OpportunityFieldHistory> history = [Select Id, CreatedById, CreatedDate,
 Field, NewValue, OldValue from OpportunityFieldHistory Where Field = 'StageName'];
2
  • Could you point me to the documentation about enabling field history(the collection is empty now)? Cannot find it with google.
    – grass
    Commented Sep 9, 2015 at 17:02
  • 1
    Sure. help.salesforce.com/apex/… Commented Sep 9, 2015 at 17:19
13

You can use the OpportunityHistory object

SELECT StageName, Probability, Amount FROM OpportunityHistory where OpportunityId = {YourOppId}

if you are doing it in Apex, you can use relationship query like this

List<Opportunity> opps = [SELECT id,
        (SELECT StageName,Probability,Amount FROM OpportunityHistories) 
        FROM Opportunity];

List<OpportunityHistory> histories = opp.OpportunityHistories 

you can also use OpportunityFieldHistory which will give you the history of all the fields on the opportunity for which field tracking is enabled.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .