5

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:

  1. I click New
  2. I get to record type selection page(URL: https://cs18.salesforce.com/setup/ui/recordtypeselect.jsp?ent=Account&nooverride=1 , so far so good)
  3. I select a record type and click Continue
  4. The page refreshes and has this URL for a (very tiny) moment: https://cs18.salesforce.com/setup/ui/recordtypeselect.jsp
  5. 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?

1 Answer 1

6

Ok, so I found the problem. It was in this line:

return new PageReference('/setup/ui/recordtypeselect.jsp?ent=Account&nooverride=1');

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):

return new PageReference('/setup/ui/recordtypeselect.jsp?ent=Account&nooverride=1&save_new_url=%2F001%2Fe');

And actually I don't even need this if anymore(that checks for RecordType parameter), so the code was reduced to this:

public PageReference redirect() {
    if (!shouldSkipRecordTypeSelection(u)) { // a simple if that checks user's profile and returns true if record type selection should be skipped
        return new PageReference('/setup/ui/recordtypeselect.jsp?ent=Account&nooverride=1&save_new_url=%2F001%2Fe');
    }
    return null;
}

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 with ent=01I8E0000008XjE

2
  • Nice Ans. One thing I have noticed when we use /setup/ui/recordtypeselect.jsp?ent=Account&nooverride=1 for custom object. It won't work.. /setup/ui/recordtypeselect.jsp?ent=CustomObject__c&nooverride=1
    – Ratan Paul
    Mar 22, 2016 at 6:39
  • 1
    @sfdcweb you are right. I just updated the answer to cover custom objects too
    – Novarg
    Mar 22, 2016 at 7:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .