0

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');

      If(!string.isBlank(idContact)){

          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);
        ReturnPage.setRedirect(true);
        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"/>    
        <div></div>

        </apex:pageblockbuttons>

        <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:inputField>

            <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"/>

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

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()) {
                 mapofCaseRecordTypeNameandId.put(rti.getName(),rti.getRecordTypeId());
                If(rti.getName() != 'sOpportunity'){
                    blnSalesRecordType = True;
                }Else{
                    blnSalesRecordType = False;
                }
            }
        }

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.