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
BenPBenP 

Redirect VF to record type selector

I'm attempting to override a standard New button to prevent all but two profiles from creating from that standard button.  The problem I'm having is that when I get to the record type selection screen, I click continue and then see the same record type selection screen.  After clicking continue again, all is well.  I think I'm hitting the standard record type selection page first then I'm being sent to the URL in my extension.

I'm totally a script kitty, but I'm trying so forgive my code if it's bad.

VF page:
<apex:page standardController="Work_Orders__c" extensions="newWOextension" action="{!getRedir}" >


    <apex:pageBlock rendered="{!showError}" >
        <apex:pageBlockSection ></apex:pageBlockSection>
        <font color="red" size="4">
        <apex:outputText value="Please create new work orders using the button on cases."  >
        </apex:outputText>
        </font>
        <apex:form >
        <apex:commandButton value="Go Back" action="{!Cancel}"/>
        </apex:form>
    </apex:pageBlock>


</apex:page>
Extension:
public without sharing class newWOextension {

    public boolean showError{ get; set; }
    public Work_Orders__c record{ get; set; }
    
    public newWOextension(ApexPages.StandardController controller) {
        this.record = (Work_Orders__c)controller.getRecord();
    }

    public PageReference getRedir() {

        User u = [Select Profile.Name from User where Id = :Userinfo.getUserId()];
    showError=true;
        
        PageReference newPage;
        //if the current users profile is not a warranty admin or system admin, show the error message
        if (u.Profile.Name != 'Warranty Administrator' && u.Profile.Name != 'System Administrator' ) {
            showError = true;
            return null;
        } 
        //otherwise send them to the recordtype selection screen for work orders
        else {
            showError=false;
            
            return new PageReference('/setup/ui/recordtypeselect.jsp?ent=01I7000000084o3&retURL=%2Fa0V%2Fo&save_new_url=%2Fa0V%2Fe%3FretURL%3D%252Fa0V%252Fo&nooverride=1');
             }
    

    }

    private final ApexPages.StandardController controller;

}


 
Best Answer chosen by BenP
BenPBenP
I appreciate the response.  Maybe I don't fully understand it, but it doesn't seem to help my situation. 

I did resolve this though with the class below.  The user clicks the new button, if they are one role they see an error message, otherwise they are prompted to select their record type, that data is grabbed from the url and they are redirected to the record edit page with the captured parameters added to the end.  Credit to Mike Topalovich and his blog here (http://www.topalovich.com/2009/08/force-com-tip-new-button-override-to-assign-visualforce-page-to-specific-record-type-using-native-apex-code/).
 
public without sharing class newWOextension {

    public boolean showError{ get; set; }
    public Work_Orders__c record{ get; set; }
    public String retURL {get; set;}
    public String saveNewURL {get; set;}
    public String rType {get; set;}
    public String cancelURL {get; set;}
    public String ent {get; set;}
    public String confirmationToken {get; set;}
    
    public newWOextension(ApexPages.StandardController controller) {
        this.record = (Work_Orders__c)controller.getRecord();
        this.controller = controller;

    retURL = ApexPages.currentPage().getParameters().get('retURL');
    rType = ApexPages.currentPage().getParameters().get('RecordType');
    cancelURL = ApexPages.currentPage().getParameters().get('cancelURL');
    ent = ApexPages.currentPage().getParameters().get('ent');
    confirmationToken = ApexPages.currentPage().getParameters().get('_CONFIRMATIONTOKEN');
    saveNewURL = ApexPages.currentPage().getParameters().get('save_new_url');
    
    }

    public PageReference getRedir1() {

        User u = [Select Profile.Name from User where Id = :Userinfo.getUserId()];
    showError=true;
        
        PageReference newPage;
        //if the current users profile is not a warranty admin or system admin, show the error message
        if (u.Profile.Name != 'Warranty Administrator' && u.Profile.Name != 'System Administrator' ) {
            showError = true;
            return null;
        } 
        //otherwise send them to the edit work orders url with their record type selection parameters tacked on the end
        else {
            showError=false;
            
            
            PageReference returnURL;
            
            returnURL = new PageReference('/a0V/e');
            
            returnURL.getParameters().put('retURL', retURL);
            returnURL.getParameters().put('RecordType', rType);
            returnURL.getParameters().put('cancelURL', cancelURL);
            returnURL.getParameters().put('ent', ent);
            returnURL.getParameters().put('_CONFIRMATIONTOKEN', confirmationToken);
            returnURL.getParameters().put('save_new_url', saveNewURL);
            returnURL.getParameters().put('nooverride', '1');
            
            returnURL.setRedirect(true);
            return returnURL;

            
        }
    

    }

    private final ApexPages.StandardController controller;

}

My test class:
static testMethod void TestNewWOextension() {
        //try to create a work order as normal user-should get error page,showError = true
        //try to create a work order as a warranty admin-should get recordtype selection page,showError = false
        Test.startTest();
        
        Work_Orders__c acc = new Work_Orders__c(serial_number__c = 'Test Account');
 
        ApexPages.Standardcontroller stdController = 
                new ApexPages.Standardcontroller(acc);
        newWOextension controller = new newWOextension(stdController);
        controller.getRedir1();
        System.assertEquals(false, controller.showError);
 
        User nonAdmin = [select id, Name from User where UserRole.Name = 'Tech Service User' AND IsActive = TRUE limit 1];
        
        System.runAs(nonAdmin)
        {
            controller.getRedir1();
        }
        System.assertEquals(true, controller.showError);
        /*stdController = 
                new ApexPages.Standardcontroller(new Account());
        controller = new newWOextension(stdController);
 
        System.assertEquals(null, controller.getRedir());
        */
        
    Test.stopTest();
    }

 

All Answers

Ajay K DubediAjay K Dubedi
Hello ,
You can always create your own custom URL Parameters. For example, when you create the URL for your button, just write your own parameter like:
string customParameter = 'My Record Type';
pageReference pgRef = new pageReference ('/apex/myVFPage?customParam='+customParameter);
Then in your VF page's initilization, grab the parameter and use it however you wish:

public class Cntrlr_myVFPage{
    private Task myTask;

    public Cntrlr_myVFPage(){
        string recordTypeName=ApexPages.currentPage().getParameters().get('customParam');
        myTask = new Task();
        myTask.RecordTypeId =[Select Id,SobjectType,Name From RecordType Name =:recordTypeName and SobjectType ='Task'  limit 1].Id;
    }

}

Thanks
BenPBenP
I appreciate the response.  Maybe I don't fully understand it, but it doesn't seem to help my situation. 

I did resolve this though with the class below.  The user clicks the new button, if they are one role they see an error message, otherwise they are prompted to select their record type, that data is grabbed from the url and they are redirected to the record edit page with the captured parameters added to the end.  Credit to Mike Topalovich and his blog here (http://www.topalovich.com/2009/08/force-com-tip-new-button-override-to-assign-visualforce-page-to-specific-record-type-using-native-apex-code/).
 
public without sharing class newWOextension {

    public boolean showError{ get; set; }
    public Work_Orders__c record{ get; set; }
    public String retURL {get; set;}
    public String saveNewURL {get; set;}
    public String rType {get; set;}
    public String cancelURL {get; set;}
    public String ent {get; set;}
    public String confirmationToken {get; set;}
    
    public newWOextension(ApexPages.StandardController controller) {
        this.record = (Work_Orders__c)controller.getRecord();
        this.controller = controller;

    retURL = ApexPages.currentPage().getParameters().get('retURL');
    rType = ApexPages.currentPage().getParameters().get('RecordType');
    cancelURL = ApexPages.currentPage().getParameters().get('cancelURL');
    ent = ApexPages.currentPage().getParameters().get('ent');
    confirmationToken = ApexPages.currentPage().getParameters().get('_CONFIRMATIONTOKEN');
    saveNewURL = ApexPages.currentPage().getParameters().get('save_new_url');
    
    }

    public PageReference getRedir1() {

        User u = [Select Profile.Name from User where Id = :Userinfo.getUserId()];
    showError=true;
        
        PageReference newPage;
        //if the current users profile is not a warranty admin or system admin, show the error message
        if (u.Profile.Name != 'Warranty Administrator' && u.Profile.Name != 'System Administrator' ) {
            showError = true;
            return null;
        } 
        //otherwise send them to the edit work orders url with their record type selection parameters tacked on the end
        else {
            showError=false;
            
            
            PageReference returnURL;
            
            returnURL = new PageReference('/a0V/e');
            
            returnURL.getParameters().put('retURL', retURL);
            returnURL.getParameters().put('RecordType', rType);
            returnURL.getParameters().put('cancelURL', cancelURL);
            returnURL.getParameters().put('ent', ent);
            returnURL.getParameters().put('_CONFIRMATIONTOKEN', confirmationToken);
            returnURL.getParameters().put('save_new_url', saveNewURL);
            returnURL.getParameters().put('nooverride', '1');
            
            returnURL.setRedirect(true);
            return returnURL;

            
        }
    

    }

    private final ApexPages.StandardController controller;

}

My test class:
static testMethod void TestNewWOextension() {
        //try to create a work order as normal user-should get error page,showError = true
        //try to create a work order as a warranty admin-should get recordtype selection page,showError = false
        Test.startTest();
        
        Work_Orders__c acc = new Work_Orders__c(serial_number__c = 'Test Account');
 
        ApexPages.Standardcontroller stdController = 
                new ApexPages.Standardcontroller(acc);
        newWOextension controller = new newWOextension(stdController);
        controller.getRedir1();
        System.assertEquals(false, controller.showError);
 
        User nonAdmin = [select id, Name from User where UserRole.Name = 'Tech Service User' AND IsActive = TRUE limit 1];
        
        System.runAs(nonAdmin)
        {
            controller.getRedir1();
        }
        System.assertEquals(true, controller.showError);
        /*stdController = 
                new ApexPages.Standardcontroller(new Account());
        controller = new newWOextension(stdController);
 
        System.assertEquals(null, controller.getRedir());
        */
        
    Test.stopTest();
    }

 
This was selected as the best answer