2

I have one custom object related to Account. The custom object has 3 record types and I would like to control what record type opens for the user. Record Type (Master) opens an older force.com layout. Record type 2 opens a newer force.com object layout with 5 fields. Record type 3 should open a visual force page with many more fields.

What is the best way to allow a user to open a record that used any one of these record types/page layout assignments? Its a mix of force.com and visualforce so not sure what others have done in this case that I can leverage.

I am not trying to restrict access but merely allow the proper layout to be displayed based on the record type.

Updated Code:

I created a formula field on the customer Account Summary object to calculate the RecordTypeName__c rather than use the controller to figure that out.

The controller loads OK but when I add the jscript to the VisualForce page the page starts looping reloading itself and the VF page shows this error:

'Attempt to de-reference a null object'

<script type='text/javascript'>
    window.onload= function Redirect()
    {
        redirection();
    }
</script>
 <apex:actionFunction name="redirection" action="{!redirectAcctNewPage}" />

//Open and view page according to record type. There are multiple CRE versions with seperate record types. 
    public PageReference redirectAcctNewPage() {
    PageReference pageRef = null;    
    //Get RecordType  //used to redirect to correct pagetype when loaded.   
    this.RecordTypeName = this.AccountSummary.RecordTypeName__c;

    try {
        if(this.RecordTypeName.equals('New_Account_Summary'))
        {   
            pageRef = new PageReference('/apex/PROD_UW_AccountSummary');
            pageRef.getParameters().put('id', this.AccountSummary.Id);
            pageRef.setRedirect(true);  
        }
        else 
        {
            pageRef = new PageReference('/' + this.AccountSummary.Id);
            pageRef.getParameters().put('inline', '0');
            pageRef.setRedirect(true);   
        }

        } catch (Exception ex) {
            ApexPages.addMessages(ex);
        }
           return pageRef;
        }
5

Assuming you want to access the record from List View of that Custom Object or from the related list of Account object.

Create a formula field with the same Name and use this code.

For example if recordtype is Accounting it will display standard page and for other recordtype it will open visualforce page.

   HYPERLINK(
   IF( RecordType.DeveloperName = 'Accounting', '/'+ Id , '/apex/yourVisualforcePage?id=' + Id
   ) 
    ,Name 
    , "_Parent" 
    )

Update

Override view action with Visualforce routing page like this (given an example taking Account object, logic is same, just you need to replace with your custom object):

<apex:page standardController="Account" id="myPage" cache="true" showHeader="false" extensions="AccountController">
   <meta http-equiv="refresh" content="N"/>
    <apex:form id="myform">
        <script type='text/javascript'>
            window.onload= function Redirect()
            {
                redirection();
            }
        </script>
         <apex:actionFunction name="redirection" action="{!redirectAcctNewPage}" />
    </apex:form>
 </apex:page>

Controller for routing

public Account acct {get; set;}   
public AccountController(ApexPages.StandardController controller)     
{   
    if(ApexPages.currentPage().getParameters().get('id') !=null)
    {
        acct = [Select id, RecordTypeId, RecordType.Name from Account where id=:ApexPages.currentPage().getParameters().get('id')];
    }        
}
 public PageReference redirectAcctNewPage() {

    if(acct.RecordType.Name.equals('Accounting'))
    {   
        return (new ApexPages.StandardController (new Account(Id=acct.Id))).view();
    }
    else 
    {
        pgrf = new PageReference('/apex/myVisualForcePage');
        pgrf.getParameters().put('id', ApexPages.currentPage().getParameters().get('id'));
        pgrf.setRedirect(true);
        return  pgrf;
    }
}
| 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.