0

I am trying to build below VF page,PFA.I want to fetch those picklist values dynamically without hardcoding. I need to assign VF page to one of the record type. and rest all should navigate to standard page. Basically i am overriding New button so that i can assign the record type to VF page,Inorder to that i created below page.

VF page:

<apex:page standardController="Opportunity" e>
  <apex:form >
    <apex:pageBlock title="Opportunity Edit" mode="save">
      <apex:pageBlockButtons location="bottom">

        <apex:commandButton action="{!Cancel}" value="Cancel"/>
      </apex:pageBlockButtons>
<p>
    Select a record type for the new opportunity. To skip this page in the future, change your record type settings on your personal setup page.
        </p>
        <br/> 
         <apex:pageBlockSection title="Select Opportunity Record Type">
           <apex:selectList  label="Record Type of new record" value='{!Opportunity.recordtype.pick}'>
            <apex:selectOptions value="{!TypeOptions}" />
            </apex:selectList>           
        </apex:pageBlockSection>
      </apex:pageBlock>    
    </apex:form>
</apex:page>

Controller:

public class NewExtension {

    public Opportunity Opp{get;set;}
    public string pick{get;set;}

    public NewExtension(ApexPages.StandardController controller)
    {

        Opp = (Opportunity)controller.getRecord();
    }

    public PageReference save()
    {
        RecordType rt;
        rt = [Select Id,DeveloperName From RecordType Where sObjectType = 'Opportunity'];

        if(pick == 'PM-I Opportunity' &&  rt.DeveloperName == 'PM_I_Opportunity' )
        {
        return new PageReference('/apex/OppExtension');
        }   
        else

        return new PageReference('/006/e?retURL=%2F006%2Fo&RecordType='+rt.id+'&ent=Opportunity');
    }
   public PageReference Cancel()
   {
        return ApexPages.currentPage();
   }


 public List<SelectOption> getTypeOptions(){
          List<SelectOption> options = new List<SelectOption>(); 
          List<RecordTypeInfo> rtinf = Opportunity.SObjectType.getDescribe().getRecordTypeInfos();
           for (RecordTypeInfo rtf : rtinf) 
           {
                   if(rtf.isAvailable() && rtf.getName() ) 
{                
                  options.add(new SelectOption(rtf.getRecordTypeId() , rtf.getName()));   
               return options;

        }
    }

enter image description here

| improve this question | | | | |
0

Currently pick is null. You need to assign the recordtype value before using it.

public PageReference save()
    {
        RecordType rt;
        rt = [Select Id,DeveloperName From RecordType Where sObjectType = 'Opportunity'];
        pick = Opp.recordtype.LC_FPD_PR_Opportunity;

        if(pick == 'PM-I Opportunity' &&  rt.DeveloperName == 'PM_I_Opportunity' )
        {
            return new PageReference('/apex/OppExtension');
        }   
        else

        return new PageReference('/006/e?retURL=%2F006%2Fo&RecordType='+rt.id+'&ent=Opportunity');
    }
| improve this answer | | | | |
  • @Derek I have added the List<selectOption> method and used the same in VF. Error- no viable alternative at character ' ' in for loop. Please help – User 7864 Jun 30 '17 at 7:37
0

You can fetch recordTypes through SObject describe information like so

// This brief example uses getRecordTypeInfosById(), but you could also use
//   getRecordTypeInfosByName().
// Predictably, if you use getRecordTypeInfosByName(), the key of the resulting map
//   is a String, rather than an Id
Map<Id, Schema.RecordTypeInfo> recTypes = Opportunity.SObjectType.getDescribe().getRecordTypeInfosById();

The one real 'gotcha' to this method is that the 'name' that you can get from the RecordTypeInfo class is the label (not the developer name).

If you'll just be using this to generate data for an <apex:selectList>, you could simply use getRecordTypeInfos() to get a List of record type info records, and loop over them to generate your SelectOption records.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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