I am overriding the New Button on My custom object which is having different record types. I need to display different sections based on their record type. Right now I am rendering pageblock sections using Record type Id, which is not a best practice, but when I try to render the same on the basis of record type Name , it just doesn't works . Please provide some help, as hardcoding record type Id will cause issue while moving the page into production org.
MY code is as follows :
<apex:page standardcontroller="Test_Object_1__c" extensions="Demo_EXT" action="{!checkRecordType}">
<apex:messages />
<apex:sectionheader title="{!$ObjectType.Test_Object_1__c.label} Edit" subtitle="{!IF(ISNULL(Test_Object_1__c.Name), 'New Service',Test_Object_1__c.Name)}"/>
<apex:form >
<apex:pageblock mode="edit" title="{!$ObjectType.Test_Object_1__c.label} Edit">
<apex:pageblockbuttons >
<apex:commandbutton value="Save" action="{!Save}"/>
<apex:commandbutton value="Cancel" action="{!Cancel}"/>
</apex:pageblockbuttons>
<apex:pageblocksection title="Information" showheader="true" columns="2">
<apex:inputfield value="{!Test_Object_1__c.Name__c}"/>
<apex:outputfield value="{!Test_Object_1__c.OwnerID}"/>
<apex:inputfield value="{!Test_Object_1__c.Password__c}" required="false"/>
<apex:pageblocksectionitem />
</apex:pageblocksection>
<apex:pageblocksection title="Additional Information" showheader="true" columns="2" rendered="{!Test_Object_1__c.RecordTypeId='01228000001DfoR'}">
<apex:inputfield value="{!Test_Object_1__c.Text_1__c}"/>
<apex:inputfield value="{!Test_Object_1__c.Text_2__c}" required="false"/>
<apex:pageblocksectionitem />
</apex:pageblocksection>
<apex:pageblocksection title="System Information" showheader="true" columns="2">
<apex:pageblocksectionitem />
<apex:outputfield value="{!Test_Object_1__c.RecordTypeId}"/>
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>
And the controller :
public class Demo_EXT{
String sRetURL = System.currentPageReference().getParameters().get('sRetURL');
String strRecordId;
public String strDevName;
public Demo_EXT(ApexPages.StandardController std) {
strRecordId = std.getId();
//system.debug('>>>>>>>>sRecordId '+strRecordId);
}
public pagereference checkRecordType(){
String objCustomobjectRecordType = ApexPages.currentPage().getParameters().get('RecordType');
system.debug('>>>>>>>>objCustomobjectRecordType '+objCustomobjectRecordType );
If(objCustomobjectRecordType != '01228000001Dflc' && objCustomobjectRecordType != '01228000001DfoR')
{
return new PageReference('/a1C/e?nooverride=1');
}
return null;
}
public PageReference urlRedirect() {
String strServiceRecType = ApexPages.currentPage().getParameters().get('RecordType');
system.debug('>>>>>>>>strServiceRecType '+strServiceRecType );
if (strRecordId == null && strServiceRecType != null){
strDevName='';
try {
strDevName= [Select Id, Name, DeveloperName from Recordtype WHERE SObjectType = 'Test_Object_1__c' and Id = :strServiceRecType ].DeveloperName;
system.debug('>>>>>>>>strDevName'+strDevName);
}catch (QueryException e){
System.debug('Error' + e);
}
if (strDevName== 'Type_2' || strDevName== 'Type_3'){
system.debug('>>>>>>>>Inside if strDevName'+strDevName);
return new PageReference('/apex/DemoVF?RecordType='+strServiceRecType);
}
else {
system.debug('>>>>>>>>Inside else'+strDevName);
return new PageReference('/a0G/e?sRetURL=%2Fa0G%2Fo&RecordType=012580000000PIb&ent=Service__c&nooverride=1');
}
}
return null;
}
}