2

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;
  }


}
1

You should change your without using hardcoded recordtypeIds. Also, recordtype ids are available from metadata. No need to use SOQL query for that.

So, change your controller like this. I have put a reusable method to retrieve recordtype name from id.

public class Demo_EXT{
String sRetURL = System.currentPageReference().getParameters().get('sRetURL');
String strRecordId;
public String strDevName {get;set;}

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

    //verify the recordType Name here
      if(getRecordTypeNameById('Test_Object_1__c', objCustomobjectRecordType) != '<put recordtype name>')
      {
            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 = getRecordTypeNameById('Test_Object_1__c', strServiceRecType);

       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;
  }

   public static String getRecordTypeNameById(String objectName, Id strRecordTypeId)
    {
        return Schema.getGlobalDescribe().get(objectName).getDescribe().getRecordTypeInfosById().get(strRecordTypeId).getName();
    }
}

Visualforce

In the rendered condition, put the recordtype name. This way at any environment code will work.

<apex:pageblocksection title="Additional Information" showheader="true" columns="2" rendered="{! '<put recordtype name'}">
    <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>
  • Hi @Sanatnu, Thanks for your suggestion, but it still doesn't work in visualforce , <apex:pageblocksection title="Additional Information" showheader="true" columns="2" rendered="{!IF(strDevName=='Type 3',true,false)}">, I've tried it the way you said, as well as the above mentioned code. – Shobhit Saxena Apr 14 '17 at 8:51
  • 1
    I have updated code regarding strDevName declaration and value assignment. Can you check if strDevName is displaying correctly or not? Then test the rendering part – Santanu Boral Apr 14 '17 at 14:34
  • Yes, strDevName is coming out correctly, Santanu. – Shobhit Saxena Apr 17 '17 at 18:19

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.