0
<apex:page standardController="lead" extensions="LeadToMerchantController"  action="{!convert}">

</apex:page>

I'm trying to retrieve the record type Name of the Lead object in the following piece of code:

    Lead objLead;
//constructor
        public LeadToMerchantController(ApexPages.StandardController controller) {    
                objLead = (Lead)controller.getRecord();
            }
        ...
            String leadType = objLead.RecordType.Name;
                    System.debug('leadType ' + leadType);

but instead I receive 'Null' as a value of 'leadType'.

Please advise how to retrieve the name of the Lead record type?

2 Answers 2

0

For your page, make sure you include the fields you need:

<apex:page standardController="lead" extensions="LeadToMerchantController"  action="{!convert}">
    <apex:outputText rendered="false">
      {!Lead.RecordType.Name}
    </apex:outputText>
</apex:page>

This will automatically include the value as part of your standard controller query.

For the unit test, make sure you construct a standard controller that uses a query that includes the field:

Lead record = new Lead(...);
insert record;
record = [SELECT RecordType.Name... FROM Lead];
ApexPages.StandardController sc = new ApexPages.StandardController(record);
LeadToMerchantController ltmc = new LeadToMerchantController(sc);
...

Note that, when using this technique, field level security is enforced, meaning that if a user can't see a field, it won't be included in the standard controller query and will thus be null in your code. You can't do this for Record Type, as far as I'm aware, but you should be aware of this limitation when automatically including standard and custom fields that are not globally required/visible.

1

You can use the ApexPages.StandardController method addFields to force the controller to query additional fields which are not referenced in the page which uses it. Note you cannot call this method from a test, so it is one of the very few cases where you need to check Test.isRunningTest.

public MyExtension(ApexPages.StandardController controller)
{
    if (!Test.isRunningTest()) controller.addFields(['RecordType.Name']);
    record = (Lead)controller.getRecord();
}
3
  • Thank you so much for your suggestion and your comment. However I need it exactly for a test method. Any ideas how to accomplish that?
    – lemdev
    Mar 6, 2018 at 22:47
  • You haven't shared any of your context, making it very difficult to assist you. I suggest you re-query for your record before passing it in to your standard controller.
    – Adrian Larson
    Mar 6, 2018 at 22:54
  • I have a button in the standard lead page and the button is opening a custom VF page in the very beginning of the post. Via the custom VF page I pass the Lead object to the controller. I'm not sure but please explain how to make re-query for the record before passing it to the controller?
    – lemdev
    Mar 6, 2018 at 23:04

You must log in to answer this question.

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