2

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

  1. 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); 
    }
}
4
  • Are you passing RecordType parameter with url
    – Reshma
    Jan 23, 2017 at 8:05
  • No Reshma , If user clicks the new button from related list (on parent record detail page), the retURL parameter will be set as the parent Record id . I am using this Id to query the Parent Record and parent record's RecordType.
    – Rajeev
    Jan 23, 2017 at 9:43
  • Did you override new button of Child object with vf page?
    – Reshma
    Jan 23, 2017 at 9:50
  • Yes. Please read "What I have done so far" in the description .
    – Rajeev
    Jan 23, 2017 at 9:51

2 Answers 2

1

May be you can try creating a javascript Listview button with below code and enable that on the related list.

For my implementation:

I have two objects Campsite__c and Sample__c
Sample__c has a lookup to Campsite__c


{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
var recId = window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
var recName = sforce.connection.query("select Id,Name from Campsite__c where id='"+recId +"'").getArray('records')[0].Name;

var lkFieldId = 'CF'+'00N2800000IssOj';//Field id of the lookup field
var objId = 'a0B';//Relate List object type
var recordTypeId = '01228000000yWJ3';//Record type to be assigned

window.location.href = '/'+objId+'/e?'
                            +lkFieldId+'='+recName
                            +'&'+lkFieldId+'_lkid='+recId 
                            +'&RecordType='+recordTypeId 
                            +'&retURL=/'+recId ;


Though the record type id, object id and field id will be constant but you can try accessing them dynamically.

2
  • Hi Saumya, Thanks for the Idea , although I desire to navigate to another VF, your idea has of course showed me the path to do so. I am answering my question with the updated code .
    – Rajeev
    Jan 24, 2017 at 9:01
  • Hi Saumya, can you please tell how do we dynamically get field Id of lookup field ?
    – Rajeev
    Jan 25, 2017 at 10:42
0

Create a JavaScript List view button to child object and use below code to navigate to the intermediate page.

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}
var recId = window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
var recName = sforce.connection.query("select Id,Name,RecordTypeId from Parent__c where id='"+recId +"'").getArray('records')[0].RecordTypeId;
var ParentRecordTypeAId= sforce.connection.query("SELECT Id,Name FROM RecordType  WHERE SobjectType = 'Parent__c' AND name = 'RecordType A'").getArray('records')[0].Id;
var ParentRecordTypeBId = sforce.connection.query("SELECT Id,Name FROM RecordType  WHERE SobjectType = 'Parent__c' AND name = 'RecordType B'").getArray('records')[0].Id;
var ChildRecordTypeAId = sforce.connection.query("SELECT Id,Name FROM RecordType  WHERE SobjectType = 'Child__c' AND name = 'RecordType A'").getArray('records')[0].Id;
var ChildRecordTypeBId = sforce.connection.query("SELECT Id,Name FROM RecordType  WHERE SobjectType = 'Claim__c' AND name = 'RecordType B'").getArray('records')[0].Id;
if(recName == ParentRecordTypeAId){
window.location.href = '/apex/intermediatePage?'
                    +'&RecordType='+ChildRecordTypeAId
                    +'&retURL=/'+recId ;
}else if(recName == ParentRecordTypeBId){
window.location.href = '/apex/intermediatePage?'
                        +'&RecordType='+ChildRecordTypeBId 
                        +'&retURL=/'+recId ;
}else{ 
alert("Record Type of this record is not valid ... " ); 
}

Also remove the below part from intermediate page as now the logic is taken care in JS List view button.

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



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

    }

You must log in to answer this question.

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