function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BrivoSFDCBrivoSFDC 

Redirecting to VF Page for Specific Record Type ID

Hello all,

 

I'm hoping that I can get some help with this code. I'm trying to write a visualforce page to redirect users to an error page for leads with a specific record type id. I seem to have it working for the System Administrator profile, but not for others. Here's what I have so far:

 

public with sharing class DispatcherLeadViewController {
    
    private ApexPages.StandardController controller = null;
    
    public DispatcherLeadViewController (ApexPages.StandardController controller){
        this.controller = controller;

    }
    
    public PageReference redirectDefaultLead(){
        
        Lead l = [Select id, recordtypeid from Lead where Id = :ApexPages.currentPage().getParameters().get('id')];
        
        if (l.recordtypeid == '012C0000000QAWZ') {
        
        return Page.defaultleadredirect;   
    
        } else {
               return new PageReference ('/' + l.id + '?nooverride=1');
        }
    }
}

 Code for page:

 

<apex:page standardController="Lead" extensions="DispatcherLeadViewController" 
  action="{!redirectDefaultLead}">
</apex:page>

I'm not sure what I'm missing. Any help would be MUCH appreciated.

 

Thanks!!!

Best Answer chosen by Admin (Salesforce Developers) 
BrivoSFDCBrivoSFDC

I finally figured out how to redirect the page based on Role and Record Type. Below is the code I used in case anyone is interested:

 

public with sharing class DispatcherLeadViewController {
    
    private ApexPages.StandardController controller = null;
    
    public DispatcherLeadViewController (ApexPages.StandardController controller){
        this.controller = controller;

    }
    
    public PageReference redirectDefaultLead(){
        
        //Get the role of the current user and the lead record type ID.
        User CurrentUser = [Select id, name, UserRoleId from User where Id = :Userinfo.getUserId()];
                
        Lead l = [Select id, recordtypeid from Lead where Id = :ApexPages.currentPage().getParameters().get('id')];
        
        //All leads are viewable for THIS ROLE ID.
        if (CurrentUser.UserRoleId == 'ROLE ID') {
        
        return new PageReference ('/' + l.id + '?nooverride=1'); }
        
        {
        
        //Only leads without this record type ID selected are viewable for all other role IDs.
        if ((CurrentUser.UserRoleId != 'ROLE ID') && (l.recordtypeid != 'RECORD TYPE ID')) {

        return new PageReference ('/' + l.id + '?nooverride=1'); }

        else {
               
               return Page.defaultleadredirect;  
             }
        }
    }
}

 

All Answers

Rakesh Aggarwal.ax1406Rakesh Aggarwal.ax1406

Are you getting any error for non Sys Admin User? Have you tried putting some debug to check if you are getting correct recordtype id value for Non Sys Admin User?

 

BrivoSFDCBrivoSFDC

Hi Rakesh,

 

I get the following error when logged in under a different profile:

 

Insufficient Privileges

You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary. 

 

Any ideas?

apaiapai
Have you checked the VF page security - to verify if the page is enabled for the necessary user profiles ? On the VF Pages, click on Security in the Action column next to the page and select the profiles that need access to this page. Hope this helps !
BrivoSFDCBrivoSFDC

That was it! Thanks so much!

 

I do have one other question however. If I needed to only have the redirect for certain users, and not for others, how would I go about adding that? Specifically, I'm looking to have all roles except for one to be redirected. I'm still new to Apex and not quite sure how to do that.

Gunners_23Gunners_23

// Get the role of the current user

 

User CurrentUser = [SELECT ID,Name,UserRoleID FROM USER WHERE id = :Userinfo.getUserId()];

 

if(CurrentUser.UserRoleId=='you don't want to redirect)

{

       // don't redirect

}

else

{

       //  redirect

}

BrivoSFDCBrivoSFDC

Thanks for responding! However, I'm getting "Error: Compile Error: Non-void method might not return a value or might have statement after a return statement. " for the below code. Not sure what that means. Any ideas?

 

public with sharing class DispatcherLeadViewController {
    
    private ApexPages.StandardController controller = null;
    
    public DispatcherLeadViewController (ApexPages.StandardController controller){
        this.controller = controller;

    } 
    
 
    public Pagereference redirectDefaultLead(){
        
            // Get the role of the current user.
    
    	    User CurrentUser = [Select id, name, UserRoleId from User where Id = :Userinfo.getUserId()];
    
            if (CurrentUser.UserRoleId == '00E80000000zbFt')
    
    	    {
	    
	    Lead l = [Select id, recordtypeid from Lead where Id = :ApexPages.currentPage().getParameters().get('id')];
        
            if (l.recordtypeid == '012C0000000QAWZ') {
        
            return new PageReference ('/' + l.id + '?nooverride=1');   
    
                } else {

                    return Page.defaultleadredirect;
            }
        }
    }
}

 


Gunners_23Gunners_23

public with sharing class DispatcherLeadViewController

{

private ApexPages.StandardController controller = null;

public DispatcherLeadViewController (ApexPages.StandardController controller){
this.controller = controller;

}


public Pagereference redirectDefaultLead(){

// Get the role of the current user.

User CurrentUser = [Select id, name, UserRoleId from User where Id = :Userinfo.getUserId()];

if (CurrentUser.UserRoleId == '00E80000000zbFt')

{

Lead l = [Select id, recordtypeid from Lead where Id = :ApexPages.currentPage().getParameters().get('id')];

if (l.recordtypeid == '012C0000000QAWZ')

{

return new PageReference ('/' + l.id + '?nooverride=1');

}

else

{

return Page.defaultleadredirect;
}
}

// return null ( to return to the same page) or specify any other page if the user's roleID is not 00E80000000zbFt

}
}

BrivoSFDCBrivoSFDC

Thanks again for responding. However, I'm still getting the same non-void compile error. Do I need to add a return statement after this?

 

User CurrentUser = [Select id, name, UserRoleId from User where Id = :Userinfo.getUserId()];

if (CurrentUser.UserRoleId == '00E80000000zbFt')

BrivoSFDCBrivoSFDC

I finally figured out how to redirect the page based on Role and Record Type. Below is the code I used in case anyone is interested:

 

public with sharing class DispatcherLeadViewController {
    
    private ApexPages.StandardController controller = null;
    
    public DispatcherLeadViewController (ApexPages.StandardController controller){
        this.controller = controller;

    }
    
    public PageReference redirectDefaultLead(){
        
        //Get the role of the current user and the lead record type ID.
        User CurrentUser = [Select id, name, UserRoleId from User where Id = :Userinfo.getUserId()];
                
        Lead l = [Select id, recordtypeid from Lead where Id = :ApexPages.currentPage().getParameters().get('id')];
        
        //All leads are viewable for THIS ROLE ID.
        if (CurrentUser.UserRoleId == 'ROLE ID') {
        
        return new PageReference ('/' + l.id + '?nooverride=1'); }
        
        {
        
        //Only leads without this record type ID selected are viewable for all other role IDs.
        if ((CurrentUser.UserRoleId != 'ROLE ID') && (l.recordtypeid != 'RECORD TYPE ID')) {

        return new PageReference ('/' + l.id + '?nooverride=1'); }

        else {
               
               return Page.defaultleadredirect;  
             }
        }
    }
}

 

This was selected as the best answer