[SalesForce] Get opportunity stages for specific sales process

I would like to get the available stages for a specific opportunity sales process via code.
BusinessProcess and RecordType objects are not connected to the OpportunityStage object.
The code below doesn't help either (see the debug result in the code comment):

Schema.DescribeFieldResult f = Opportunity.StageName.getDescribe();
    List<Schema.PicklistEntry> p = f.getPicklistValues();
       for(Schema.PicklistEntry e:p){
        system.debug('Pick list entry: ' + e); //Pick list entry: Schema.PicklistEntry[getLabel=Target;getValue=Target;isActive=true;isDefaultValue=false;]
    }

Do you have any suggestions?
Thanks!

Best Answer

I'm going to assume that you're trying to tie your Sales Processes in with your Business Process' RecordTypes and your Opportunity Stages so that everything is consistent and the associations are connected as originally intended.

Unfortunately, picklists cannot be selected by RecordType using Apex. That can only be done via the metadata API or using a workaround that Andrew Fawcett posted several years ago that I can't seem to locate at the moment. However, it can be found in his github repository at https://github.com/financialforcedev/apex-mdapi.

There are two other related posts that may be helpful to you. One is this Stack Overflow post with an answer involving a SOQL query: https://stackoverflow.com/questions/7810801/how-to-describe-enumerate-picklist-enties-valid-for-a-specific-record-type-in. Second is this post by @Eric that discusses the entire issue of picklists and record types along with his solution to the issues: How do I get the intersection of RecordType and Picklist values inside Apex?. Hopefully, you'll find that one of these 3 solutions will provide what you're looking for to help you reach your objective.