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;
}
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();
}