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"/>
SaveOpp()
method (rather than usingsoMyOpportunity
)?