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 Answer 1

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>
3
  • 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. Apr 14, 2017 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 Apr 14, 2017 at 14:34
  • Yes, strDevName is coming out correctly, Santanu. Apr 17, 2017 at 18:19

You must log in to answer this question.

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