Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to come with some logic to figure out what Opportunities have progressed in a week. One thing I was thinking of doing was getting the latest and earliest stages for the Opportunities from the Opportunity History table.

Then comparing the stages and this will indicate if an Opportunity has progressed or not.

So, do get the latest. I could do something like:

SELECT Amount, CloseDate, CreatedById, CreatedDate, IsDeleted, ExpectedRevenue, 
Id, OpportunityId, Probability, StageName, SystemModstamp, ForecastCategory 
FROM OpportunityHistory
WHERE OpportunityId = 'blah'
order by createdDate desc limit 1 

However, I need to do this for all Opportunities that belong to a certain bunch of Users. I am not sure how to do this. Any tips?


share|improve this question
up vote 1 down vote accepted

I would try it so:

First i would select a users, and then opportunity history.

List<Account> acc = [ Select Id From Account Where CreatedDate = LAST_WEEK ];

List<OpportunityHistory> oppHistory = [ Select OpportunityId, CloseDate, Amount 
                                        From OpportunityHistory
                                        Where Opportunity.AccountId IN :acc ];
share|improve this answer

I ended up doing it by:

List<Opportunity> opps = [SELECT id, Probability, LastModifiedDate, OwnerId, Amount,
            (SELECT StageName,Probability,SystemModstamp FROM OpportunityHistories order by SystemModstamp desc) oppHistory
            FROM Opportunity 
            where LastModifiedDate = LAST_N_DAYS:7];

and then looping throu OpportunityHistory information for the Opportunities.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.