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
JeeedeeeJeeedeee 

getParameters().get('RecordType') returns null in case of one visible record type

Hello everybody,

 

I have a problem with my code. I have created a new page for the account. I only want to show this page, for certain record types. So I created a redirect page, which redirects the user to either the original page, or the new account page. Below is my code.

 

 

	public PageReference redirectToPage() { 
		System.debug('In redirectToPage from AccountRedirectController');
		PageReference newPage = null; 
 		System.debug('Getting record type parameter:' +ApexPages.currentPage().getParameters().get('RecordType'));
		if(ApexPages.currentPage().getParameters().get('RecordType') == RECORDTYPE_OTHER_ACCOUNT.Id) {
			System.debug('Creating newPage from step with selected record types');
			newPage = Page.NewAccountPage1;
			newPage.getParameters().put('RecordType', RECORDTYPE_OTHER_ACCOUNT..Id);
		} else {
			if(ApexPages.currentPage().getParameters().get('RecordType') != null && ApexPages.currentPage().getParameters().get('RecordType') != RECORDTYPE_TP_ACCOUNT.Id) {
				System.debug('*** information **'+ApexPages.currentPage().getParameters().get('RecordType'));
				newPage = new Pagereference('/001/e?retURL=/001/o&RecordType='+ApexPages.currentPage().getParameters().get('RecordType')+'&ent=Account'); 
				newPage.getParameters().put('nooverride', '1');
			}
		}
		return newPage;
	}

 

 

The problem is with the line

 

ApexPages.currentPage().getParameters().get('RecordType')

 This returns null, when the user only has rights to one specific recordtype. While if the user has rights to multiple recordtypes, and he selects one this line always has a valid ID.

 

Why is the record type not set if the user has only access to one specific record type? And how do I get this specific recordtype in my code, so that I can build a correct redirect?

 

 

Any help would be appreciated, thanks, Jan-Dirk

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JeeedeeeJeeedeee

Hi Everybody,

 

Got it solved by creating a method which search for the default record type in case the record type is not set. The method rtInfo.isAvailable() only returns true if the specific user has rights to that record type. I have added a call to this method in the first line of my method redirectToPage().

 

Hope it helps others,

Greetings, JD

 

/**
 * This method validates wether the record type is not set. This can only happen when the user has only
 * rights to one specific record type. In that scenario the user does not see the record type selection screen.
 * In these cases the record type is not set out of the box. This method finds the appropiate record type for this user and 
 * add this as a parameter to the page.  
 */
private void setRecordTypeIncaseNotSetted() {
	// in case it's null, the user has only one record type available. 
	if (ApexPages.currentPage().getParameters().get('RecordType') == null) {
		Schema.DescribeSObjectResult describeAccount = Schema.SObjectType.Account;
		List<Schema.RecordTypeInfo> rtInfos = describeAccount.getRecordTypeInfos();
		for(Schema.RecordTypeInfo rtInfo : rtInfos) {
			// walk through the map to determine the correct record type, this is done with the isAvailable() function
			if(rtInfo.getName() != 'Master' && rtInfo.isAvailable()) {
				// below lines will only run one time only
				System.debug('The RecordType :' +rtInfo.getName() + ' and the id: ' + rtInfo.getRecordTypeId() +' is available, adding it as parameter to the page.');
				ApexPages.currentPage().getParameters().put('RecordType', rtInfo.getRecordTypeId());
			}
		}
		if (ApexPages.currentPage().getParameters().get('RecordType') == null) {
			System.debug('Still null after for, this should never happen.');
		}
	}
}

 

 

rtInfo.isAvailable()

All Answers

b-Forceb-Force

Jan,

 

I will recomand you , dont access recordtype Id in this way,

 

retrive all recordType by SOQL as follow

public PageReference redirectToPage() 
{ String Other_acc='';
String Tp_acc='';
RecordType[] rt=[Select SobjectType, Name, Id From RecordType  where SobjectType='Account'];
for(RecordTyp r :ert)
{
if(rt.Name=='RECORDTYPE_OTHER_ACCOUNT')
Other_acc=rt.Id;
if(rt.Name=='TECORDTYPE_TP_ACCOUNT')
Tp_acc=rt.Id;
}

System.debug('In redirectToPage from AccountRedirectController'); PageReference newPage = null; System.debug('Getting record type parameter:' +ApexPages.currentPage().getParameters().get('RecordType')); if(ApexPages.currentPage().getParameters().get('RecordType') == Other_acc) { System.debug('Creating newPage from step with selected record types'); newPage = Page.NewAccountPage1; newPage.getParameters().put('RecordType', Other_acc); } else { if(ApexPages.currentPage().getParameters().get('RecordType') != null && ApexPages.currentPage().getParameters().get('RecordType') != Tp_acc) { System.debug('*** information **'+ApexPages.currentPage().getParameters().get('RecordType')); newPage = new Pagereference('/001/e?retURL=/001/o&RecordType='+ApexPages.currentPage().getParameters().get('RecordType')+'&ent=Account'); newPage.getParameters().put('nooverride', '1'); } } return newPage; }

 

Hope this will help you

Thanks,

Bala

 

JeeedeeeJeeedeee

Hi Bala,

Thanks for your post, but I don't think that that will do the trick. Since I also think I already do that now, or I understand you wrong:smileysurprised:. My problem is not with retrieving an recordtype, but with retrieving the actual recordtype.

 

I cleaned my code and below is the complete code, including VF class. The problem what I have, in case the user has the record type selection screen, the record ID is set, but when he doesn't it is not set for some reason. And I don't understand this since when I do not override my new page with this VF page, I can find the record type ID in the parameters behind the URL on the standard edit screen. Also when I only have the rights for one record type.

 

VF page: ( I overrule the new button with this page)

<apex:page title="Account Edit - New Account" standardController="Account" extensions="AccountRedirectController" action="{!redirectToPage}"> 
</apex:page>

APEX

public class AccountRedirectController {
   private static final RecordType RECORDTYPE_TP_ACCOUNT = [SELECT id FROM RecordType 
                          where DeveloperName = 'MyType' and SobjectType = 'Account']; public AccountRedirectController(ApexPages.StandardController controller) {}  /* Method to return eiter a VF page, or a standard page based on actual record type */
 public PageReference redirectToPage() { PageReference newPage;       
   // Why is below parameter filled with a record type selection page, but not
   // in case I only have rights for one record type and I don't have the sel. page?
if(ApexPages.currentPage().getParameters().get('RecordType') ==
                               RECORDTYPE_TP_ACCOUNT.Id) { newPage = Page.NewAccountPage1; newPage.getParameters().put('RecordType', RECORDTYPE_TP_ACCOUNT.Id); } else { newPage = new Pagereference('/001/e?retURL=/001/o&RecordType=
                      '+ApexPages.currentPage().getParameters().
                                  get('RecordType')+'&ent=Account'); newPage.getParameters().put('nooverride', '1'); } return newPage; } }

Btw, the reason I am doing this, is that I only want to have a specific group of users(and indirect a related record type) have access to another VF page, NewAccountPage1

 

Hope it clears up the problem I am facing

 

 

NewAccountPage1
b-Forceb-Force

Could you please explain what is actualRecordType ?

 

If you want to restrict the access of VF page on basis of Profile/User, you can check same in Page Load and Redirect to some Error page

 

Thanks,

Bala

JeeedeeeJeeedeee

Bala,


Actual record type is basically the type of the record, when I create a new account. (So when I hit the new account button).

As administrator, I have rights to all recordtypes, so I got the selection screen first, I select recordtype 'MyType', hit continue, then my VF redirect page is loaded and tadaa, the actual record type is filled, and my redirect works fine :)

But as userx, I only have rights to see recordtype 'MyType' and I don't get the selection screen, but immediately goes to the VF redirect page. The actual record type is null. Now my code crashes, the record ID is not set.

But as userX, when the new button is not overrided, I definetely see the actual ID of MyType in the URL, when I create a new account using the same button.

 

So it looks like, that when I only can see one record type, the record ID is not set before loading the redirect page. But how can I figure out in that case what the record type is the user is going to create? I wonder, maybe it's already set in the account, and I can retrieve by getting the account from my standard controller... gonna try that now

b-Forceb-Force

Thanks a lot for updates

I got your point ..

 

 

JeeedeeeJeeedeee

It's driving my nuts.. tried below, and it does not work either :(

 

 

   private Account newAccount;

   public AccountRedirectController(ApexPages.StandardController controller) {
      newAccount = (Account) controller.getRecord(); 
   }
   
   public PageReference redirectToPage() { 
       // value still null when I don't have the record type selection screen :()
       // filled btw when I have the record type selection screen, just like the parameter
       System.debug('The value of newAccount is' +newAccount.RecordTypeId);

 

If anybody has an idea /workaround... would be welcome

 

JeeedeeeJeeedeee

Hi Everybody,

 

Got it solved by creating a method which search for the default record type in case the record type is not set. The method rtInfo.isAvailable() only returns true if the specific user has rights to that record type. I have added a call to this method in the first line of my method redirectToPage().

 

Hope it helps others,

Greetings, JD

 

/**
 * This method validates wether the record type is not set. This can only happen when the user has only
 * rights to one specific record type. In that scenario the user does not see the record type selection screen.
 * In these cases the record type is not set out of the box. This method finds the appropiate record type for this user and 
 * add this as a parameter to the page.  
 */
private void setRecordTypeIncaseNotSetted() {
	// in case it's null, the user has only one record type available. 
	if (ApexPages.currentPage().getParameters().get('RecordType') == null) {
		Schema.DescribeSObjectResult describeAccount = Schema.SObjectType.Account;
		List<Schema.RecordTypeInfo> rtInfos = describeAccount.getRecordTypeInfos();
		for(Schema.RecordTypeInfo rtInfo : rtInfos) {
			// walk through the map to determine the correct record type, this is done with the isAvailable() function
			if(rtInfo.getName() != 'Master' && rtInfo.isAvailable()) {
				// below lines will only run one time only
				System.debug('The RecordType :' +rtInfo.getName() + ' and the id: ' + rtInfo.getRecordTypeId() +' is available, adding it as parameter to the page.');
				ApexPages.currentPage().getParameters().put('RecordType', rtInfo.getRecordTypeId());
			}
		}
		if (ApexPages.currentPage().getParameters().get('RecordType') == null) {
			System.debug('Still null after for, this should never happen.');
		}
	}
}

 

 

rtInfo.isAvailable()
This was selected as the best answer
MLamb2005MLamb2005

Thanks for posting this JD, I was just facing the exact same problem. It's a wonky work-around for something that should be pretty straightforward.

 

I posted an idea on the Idea Exchange to try and get this resolved, please go vote! https://sites.secure.force.com/success/ideaView?c=09a30000000D9xtAAC&id=08730000000KSixAAG