1

I have a custom object called Service__c . When creating "New" Service Record, for the 'RFLEET_Option_OPP' record type only I want to override the standard Page. For other Record types the Standard Page remains. Here my code:

Apex:
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 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 = 'Service__c'  and Id = :strServiceRecType ].DeveloperName;   
            system.debug('>>>>>>>>strDevName'+strDevName);      
        }catch (QueryException e){
                System.debug('Error' + e);
        }

       if (strDevName== 'RFLEET_Option_OPP'){
           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;
  }


}

VF:

<apex:page standardcontroller="Service__c"  extensions="Demo_EXT" action="{!urlRedirect}">
<apex:messages />
    <apex:sectionheader title="{!$ObjectType.Service__c.label} Edit" subtitle="{!IF(ISNULL(Service__c.Name), 'New Service',Service__c.Name)}"/>
    <apex:form >
        <apex:pageblock mode="edit" title="{!$ObjectType.Service__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="{!Service__c.Name}"/>
                    <apex:outputfield value="{!Service__c.OwnerId}"/>
                    <apex:inputfield value="{!Service__c.Service_Type__c}" required="false"/>
                    <apex:inputfield value="{!Service__c.CurrencyIsoCode}" required="false"/>
                    <apex:inputfield value="{!Service__c.CB_Services_Repository__c}" required="false"/>
                    <apex:inputfield value="{!Service__c.Price_HT__c}" required="false"/>
                    <apex:inputfield value="{!Service__c.CB_Options_repository__c}" required="false"/>
                    <apex:inputfield value="{!Service__c.Customer_Sales_Price_HT__c}" required="false"/>
                    <apex:inputfield value="{!Service__c.Code__c}" required="false"/>
                    <apex:inputfield value="{!Service__c.Quantity__c}" required="false"/>
                    <apex:inputfield value="{!Service__c.Label__c}" required="false"/>
                    <apex:pageblocksectionitem />
                </apex:pageblocksection>
                <apex:pageblocksection title="Order Car Set" showheader="true" columns="2">
                    <apex:inputfield value="{!Service__c.Car_Set__c}" required="false"/>
                    <apex:pageblocksectionitem />
                </apex:pageblocksection>
                <apex:pageblocksection title="System Information" showheader="true" columns="2">
                    <apex:pageblocksectionitem />
                    <apex:outputfield value="{!Service__c.RecordTypeId}"/>
                </apex:pageblocksection>


                  </apex:pageblock>
    </apex:form>
    </apex:page>

Issue am facing:

When I choose the record type, other than "RFLEET_Option_OPP" It redirects to the Standard Page. Its working fine. But when I click the "RFLEET_Option_OPP" record type it redirects to the Vf page with below error

Visual force Error Cyclical server-side forwards detected: /apex/DemoVF?RecordTypeid=012580000000PIb

3

One Way I found Use action to call a method before loading your VF page completely

<apex:page action="{!checkRecordType}">

in you class check

current record recordTypeId. If recordType is different Just redirect to standard page using record Id and nooverride=1

public pagereference  checkRecordType(){
  If(objCustomobjectRecordType != 'your recordTypeId')
  {
        return new PageReference('/'+objCustomobject.Id+'nooverride=1');
  }
}

This way to view the standard detail page of the record.


For New button

You can follow the same steps as above

but in checkRecordType method Just redirect to standard edit page

public pagereference  checkRecordType(){
      If(objCustomobjectRecordType != 'your recordTypeId')
      {
            return new PageReference('/003/e?nooverride=1');
      }
    }

Here Just an example I am using 003 for contact. you can use your custom object prefix

You can get the Custom object prefix using

system.debug('========='+ OBJECTAPINAME.sobjecttype.getDescribe().getKeyPrefix());

Run this code in developer console and get the custom object prefix.

| improve this answer | |

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.