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?
Best Answer
Ok, so I found the problem. It was in this line:
When I removed the override of the new button and tried to create a new Account, I saw this URL:
https://cs18.salesforce.com/setup/ui/recordtypeselect.jsp?ent=Account&retURL=%2F001%2Fo&save_new_url=%2F001%2Fe%3FretURL%3D%252F001%252Fo
Note this save_new_url parameter in there. After I added it to my
PageReference
everything works as it is supposed to(some profiles get a custom VF page and others go to standard record type selection and standard new layout):And actually I don't even need this
if
anymore(that checks for RecordType parameter), so the code was reduced to this:EDIT
As sfdcweb mentioned is his comment, this will not work for custom objects by simple changing the ent property to
MyCustomObject__c
.To make it work for custom objects too you will have to use the 15 character id of that custom object. To find it, go to:
Then, just replace the
ent=Account
withent=01I8E0000008XjE