One of the requirements for a project is to show a custom visualforce page whenever a user clicks on the New button on Account and skip the record type selection.
That part was easy and I could override the button and skip the record type selection.
However, if a logged in user has profile P, he should instead go to standard record type selection and then to the standard new layout.
I could partially accomplish this, but then this user gets stuck in a record type selection page. Here's the apex code:
public PageReference redirect() {
if (!shouldSkipRecordTypeSelection(u)) { // a simple if that checks user's profile and returns true if record type selection should be skipped
if (ApexPages.currentPage().getParameters().get('RecordType') == null ||
ApexPages.currentPage().getParameters().get('RecordType') == '') { // so the problem is here. At this moment the URL does not have any parameters, so this check always passes
return new PageReference('/setup/ui/recordtypeselect.jsp?ent=Account&nooverride=1');
} else { // else make a page reference to new account with the selected record type id
PageReference pr = new PageReference('/' + Account.SObjectType.getDescribe().getKeyPrefix() + '/e');
Map<String, String> m = pr.getParameters();
m.putAll(ApexPages.currentPage().getParameters());
m.remove('save_new');
m.put('nooverride', '1');
return pr;
}
}
return null;
}
This method is an action of visualforce page:
<apex:page showHeader="true" sidebar="true" standardController="Account" extensions="myCustomVFPageAccExtenstion" action="{!redirect}">
So what happens when I click New with a user with profile P:
- I click New
- I get to record type selection page(URL: https://cs18.salesforce.com/setup/ui/recordtypeselect.jsp?ent=Account&nooverride=1 , so far so good)
- I select a record type and click Continue
- The page refreshes and has this URL for a (very tiny) moment: https://cs18.salesforce.com/setup/ui/recordtypeselect.jsp
- The page then redirects to: https://cs18.salesforce.com/setup/ui/recordtypeselect.jsp?ent=Account&ent=Account&RecordType=012w0000000QMrx&nooverride=1
Note the added &ent=Account&RecordType=012w0000000QMrx parameters in the link. I do need this RecordType parameter to check in apex what should I do next, but I feel that ApexPages.currentPage().getParameters()
fires at the moment when this page has no parameters at all(step 4).
So the question is: how can I spot that a record type has already been selected in apex?