I have created code to create a opportunity off of a related list. The problem however is I am unable to determine what the recordtype is. Based on the recordtype I dont want certain fields to show on the Visualforce page.

When I set the opportunity to the Standard Controller, the Opp has an ID of 0000000000000000. The Record Type is null in Apex however on the visualforce Page the record type is set.

Since I coulnt capture this in the constructor I thought I would try and capture this in the Visualforce page itsself however this seems to be null as well.

Any suggestions?

public class clsNewOpportunityOnContact {

    Public ID idContact;   
    Private Opportunity soMyOpportunity;
    Private ApexPages.StandardSetController standardSetController{get; set;}  
    Public Boolean blnSalesRecordType {get; set;}  
    Private List<Opportunity> lstLab_Assets = new List<Opportunity>();

    Public clsNewOpportunityOnContact(ApexPages.StandardSetController Controller){

      ApexPages.StandardSetController cntr = (ApexPages.StandardSetController)controller;
      idContact = ApexPages.currentPage().getParameters().get('id');


          Map<ID, Contact> mapContact = new Map<ID, Contact>();    

          This.soMyOpportunity = (Opportunity)cntr.getRecord();

          mapContact = GetParentContact(idContact);

          If(mapContact.size() > 0){

              soMyOpportunity.Contact__c = idContact; 
              This.soMyOpportunity.Ownerid =  UserInfo.getUserId();
              This.soMyOpportunity.AccountID = mapContact.get(idContact).AccountID;

    Public Static Map<ID, Contact> GetParentContact(ID idContact){

        Map<ID, Contact> mapContact = new Map<ID, Contact>

            ([Select ID, AccountID
            From Contact                                                                                  
            Where ID =: idContact 

        Return mapContact;

    Public PageReference SaveOpp(){

        soMyOpportunity.id = Null;
        Insert soMyOpportunity;  

        PageReference returnPage = new PageReference('/' + soMyOpportunity.id);
        Return returnPage; 



<apex:page standardcontroller="Opportunity" extensions="clsNewOpportunityOnContact" lightningStylesheets="true" recordSetVar="contacts">
<apex:includeLightning />
<apex:Form >

    <apex:pageblock mode="edit" title="New Opportunity">

        <apex:pageblockbuttons location="both"> 
            <apex:commandbutton value="Save" action="{!SaveOpp}"/> 
            <apex:commandbutton html-formnovalidate="formnovalidate" action="{!Cancel}" immediate="true" value="Cancel"/>    


        <apex:sectionheader title="Opportunity"/>
        <apex:pageBlockSection title="Opportunity Information" showHeader="true" columns="2" rendered="true">

            <apex:inputField value="{!Opportunity.Name}"  rendered="true"/>
            <apex:inputField value="{!Opportunity.RecordTypeId}"  rendered="true">
                <apex:actionSupport event="onchange" rerender="myform"/>

            <apex:outputField value="{!Opportunity.Contact__c}"  rendered="true"/>
            <apex:inputField value="{!Opportunity.StageName}"  rendered="true" required="true"/>

            <apex:InputField value="{!Opportunity.AccountID}"  rendered="true" required="true"/>
            <apex:InputField value="{!Opportunity.Ownerid}"  rendered="true"/>

            <apex:inputField value="{!Opportunity.Case__c}"  rendered="true"/>
            <apex:InputField value="{!Opportunity.Region__c}"  rendered="{!If(Opportunity.RecordType.DeveloperName=='SOpportunity',False,True) }" required="true"/>

  • What does it look like if you get the Opportunity from the standard controller directly in the SaveOpp() method (rather than using soMyOpportunity)? Commented Nov 7, 2019 at 15:09
  • I am not sure what you mean... Can you elaborate?
    – CodeMonger
    Commented Nov 7, 2019 at 15:17

1 Answer 1


If anyone else ran into this a way to solve it is by getting the default record type ID from the Profile. If you want the users to be able to choose then you would have to write a separate Visual force Page for that.

For(Schema.RecordTypeInfo rti : dsr.getRecordTypeInfos()) {
            If(rti.isDefaultRecordTypeMapping()) {
                If(rti.getName() != 'sOpportunity'){
                    blnSalesRecordType = True;
                    blnSalesRecordType = False;

You must log in to answer this question.

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