How to Disable Creating Specific Types of Opportunities

How to Disable Creating Specific Types of OpportunitiesHave you ever run into a situation where you want users to be able to use multiple record types in a Salesforce object, but it should be illegal to create a new record that is not their default record type? Well, I have.

Basically, there are 2 Opportunity record types: “Order” and “Renewal”. The users should be able to create a new Opportunity with the Order record type from a standard “New” button, but they shouldn’t be able to create a new Opportunity with the Renewal record type. The only way they should be able to create a Renewal Opportunity is from the “Create Renewal” custom button (which is a URL button that pre-populates the record type Id) on an Order Opportunity page. The problem is the Renewal record type is also available to the users profile, so sometimes they accidentally select the record type and create a Renewal Opp. At first, I thought I could just make the Renewal record type unavailable to the users. So, I went to the Setup > Administer > Manage Users > Profiles > select the profile of our sales users > Object Settings > Opportunities > Edit > unchecked Assigned Record Types in the Renewal record type and saved. The users no longer had to go through the record type selection page, but when they clicked the “Create Renewal” they got this error –

recordtype-unavailable

I got stuck thinking about how to hide the record type from the users for a while, but finally I found a workaround which included a button override with a Visualforce page. The Visualforce page was only a few lines long, but it needed some help from a controller extension.

The controller looks like this:

public with sharing class OppOverride {

public with sharing class OppOverride {

private final ApexPages.StandardController stdController;
Opportunity opp {get; set;}

// Constructor – controller extension
public OppOverride(ApexPages.StandardController stdController) {

this.stdController = stdController;
opp = (Opportunity)stdController.getRecord();

}

public PageReference showOrderPage() {

Id orderId = [select Id from RecordType where Name = ‘Order’ and IsActive=true and SobjectType=’Opportunity’ limit 1].Id;
// Redirect to New page with preselect record type
PageReference p = new PageReference(‘/006/e’);

Map<String, String> mapParams = p.getParameters();

mapParams.putAll(ApexPages.currentPage().getParameters());
if (orderId != null) mapParams.put(‘RecordType’, orderId);
mapParams.put(‘nooverride’, ‘1’);
mapParams.remove(‘save_new’); // Prevent the page from trying to save the opp and create a new one
mapParams.remove(‘sfdc.override’);
return p;

}

}

And the Visualforce looks like this:

<apex:page standardController=”Opportunity” extensions=”OppOverride” action=”{!showOrderPage}” />

Then I used the override option to override the standard “New” button in the Opportunity. In the Override Properties page, I selected the “OppOverride” Visualforce page and checked “Skip Record Type Selection Page”.

Override New

 

Now the users don’t have to go through the record type selection page because the New button is set to skip it and automatically set the “Order” record type for the users by default.

This skip new record type technique can be applied to any other objects that use more than one record type.

[tagline_box backgroundcolor=”” shadow=”yes” shadowopacity=”0.1″ border=”1px” bordercolor=”” highlightposition=”left” link=”https://opfocus.com/contact-us” linktarget=”_self” buttoncolor=”blue” button=”Contact Us” title=”Having trouble with this or other problems with Salesforce? OpFocus can help!” animation_type=”slide” animation_direction=”left” animation_speed=”0.7″][/tagline_box]