There are two objects X(Parent) and Y(Child),each having two recordTypes viz 'RecordType A' and 'RecordType B'.
Requirement
1.If user clicks on new button from tab of Child object, Standard recordType selection should appear asking for desired selection and according to the selection made , page should get navigated. Well, this is happening ! :)
- If user clicks the new button for Child object from related list present in Parent object record , then the code must first check the record type of Parent object record(let's say it is 'RecordType A') and without asking for user to select the record Type, it should automatically redirect to 'PageForRecordTypeA'. *This is not happening :( *
What I have done so far ?
Overridden new button at Child object and put an intermediatePage to redirect to appropriate page.
What is the Problem ?
Even though user is coming from related list of parent record having 'RecordType A' the new button shows me Record Type selection page , which is not desired
What I have achieved ?
Irrespective of which recordType User selects, it always get redirected to correct page only.
What I want to do ?
Avoid showing recordType Selection page to user if he/she is coming from related list of a parent object's record with particular Record Type.
What is the Code ?
VF Code
<apex:page standardController="Child__c" extensions="RedirectExtension" action="{!redirect}">
Controller Code
public with sharing class RedirectExtension {
public RedirectExtension(ApexPages.StandardController controller) {
}
public PageReference redirect(){
Map<String, String> strMap;
String pageName;
//Getting Record Type
map<String,Id> childRecordTypeNameToIdMap = new map<String, Id>();
for(RecordType recordTypeRecord : [SELECT Id,Name FROM RecordType WHERE SobjectType = 'Child__c']){
childRecordTypeNameToIdMap.put(recordTypeRecord.Name,recordTypeRecord.id);
}
map<String,Id> ParentRecordTypeNameToIdMap = new map<String, Id>();
for(RecordType recordTypeRecord : [SELECT Id,Name FROM RecordType WHERE SobjectType = 'Parent__c']){
ParentRecordTypeNameToIdMap.put(recordTypeRecord.Name,recordTypeRecord.id);
}
// Inherit previous parameters, more imporatntly, RecordType parameter!
string param = '';
strMap = ApexPages.currentPage().getParameters();
String[] keys = new String[]{'RecordType', 'retURL', 'cancelURL'};
for(String s : keys){
if(strMap.containsKey(S)) param += s + '=' + strMap.get(s) + '&';
}
if(param.length() > 0) param = param.substring(0, param.length()-1);
try{
Parent__c parentRecord = [ SELECT Name,id , RecordTypeId
FROM Parent__c
WHERE id =: strMap.get('retURL').remove('/')];
if(String.ValueOf(parentRecord.RecordTypeId) == ParentRecordTypeNameToIdMap.get('RecordType A')){
pageName = 'PageForRecordTypeA';
}else if(String.ValueOf(parentRecord.RecordTypeId) == ParentRecordTypeNameToIdMap.get('RecordType B')){
pageName = 'PageForRecordTypeB';
}
return new PageReference('/apex/'+pageName+'?nooverride=1&'+param);
}catch(exception e){
}
//----------------------Code related to user RecordType Selection----------------------------------------------//
if(strMap.containsKey('RecordType')){
if(strMap.get('RecordType')==childRecordTypeNameToIdMap.get('RecordType A')){
pageName = 'PageForRecordTypeA';
}else if(strMap.get('RecordType')==childRecordTypeNameToIdMap.get('RecordType B')){
pageName = 'PageForRecordTypeB';
}
}
return new PageReference('/apex/'+pageName+'?nooverride=1&'+param);
}
}
RecordType
parameter with url