Search code examples
salesforceapex-code

Salesforce: Get List of "Closed" Opportunity Stages


In Apex, I need to get a list of the Opportunity stages that are closed. These are stages that, in setup, are of the type "Closed/Won" or "Closed/Lost".

I can get a list of stage names using:

Opportunity.StageName.getDescribe().getPicklistValues()

This returns a List of Schema.PicklistEntry objects, but they do not indicate the type of stage.


Solution

  • For that you will have to query the OpportunityStage table, it has fields that flag isWon & isClosed (amongst other things) for each option.

    Such a query would look like this:

    [SELECT Id, ApiName FROM OpportunityStage WHERE IsWon = true AND IsClosed = true]