0

I am having a custom button in account object in which a VF Page is called and in that VF page i need to display all the record type of the account object as picklist. Here is my code. But i'm facing null pointer exception.

 public with sharing class AccountRecTypeChange_CLS {

public Account acc {get;set;}
public List<SelectOption> recTypeList {get;set;}
public String strRecTypeName {get;set;}
public AccountRecTypeChange_CLS(ApexPages.StandardController controller) {
 this.acc = (Account)controller.getRecord(); 
 List<RecordType> lstRec = new List<RecordType>([SELECT Name,DeveloperName FROM RecordType WHERE SObjectType='Account']);
 System.Debug('ccc'+lstRec);
 for (RecordType  a : 
        [Select Id, Name,DeveloperName FROM RecordType 
        where SobjectType = 'Account']) 
        {
            recTypeList.add(new selectOption(a.DeveloperName, a.DeveloperName ));
        }

}
}
  • Which line is throwing this exception? – Adrian Larson May 26 '17 at 16:26
  • In this line "recTypeList.add(new selectOption(a.DeveloperName, a.DeveloperName ));" – ram135868 May 26 '17 at 16:28
  • @ram135868, see my answer, in case it helps without using SOQL – Santanu Boral May 26 '17 at 17:09
0

You haven't initiated recTypeList. You can't add elements to a null collection.

recTypeList = new List<SelectOption>(); // <-- this line is missing
for (RecordType rt : [/*query*/])
{
    recTypeList.add(new SelectOption(rt.DeveloperName, rt.Name));
    // you should use Name for the display value, rather than DeveloperName
}

If you want to select the current value by default, you may need to add fields to the controller to make it query for them. Something along the following lines should work:

if (!Test.isRunningTest()) controller.addFields(new List<String>{'RecordTypeId'});

// intermediate code

for (RecordType rt : [/*query*/])
{
    // add SelectOption
    if (rt.Id == acc.RecordTypeId)
        strRecTypeName = rt.DeveloperName;
}
| improve this answer | |
  • Ufff!!! Extremely sorry for that adrian. Also can u please tell this only one thing. How to display the selected record's recordtype value as the first value in the picklist dynamically – ram135868 May 26 '17 at 16:34
  • @ram see updates. – Adrian Larson May 26 '17 at 16:51
0

You need to initialize the list before using it to hold something. You also don't need one SOQL that seems extra, so I have commented that as well in the code below.

recTypeList = new List<SelectOption>();

This line you can write at 2nd line of the constructor.

  public with sharing class AccountRecTypeChange_CLS {

  public Account acc {get;set;}
  public List<SelectOption> recTypeList {get;set;}
  public String strRecTypeName {get;set;}
  public AccountRecTypeChange_CLS(ApexPages.StandardController controller) {
   this.acc = (Account)controller.getRecord(); 
  Account acc = [select id, RecordTypeId, RecordType.DeveloperName from Account where id=: acc.id];

//new line of initialization inserted here.
recTypeList = new List<SelectOption>();

//below line will add the first value in picklist as the selected record's record type name. 
recTypeList.add(new SelectOption(acc.RecordType.DeveloperName, acc.RecordType.DeveloperName));

//below line and SOQL is not needed, as you are again doing a SOQL below for loop.
/*List<RecordType> lstRec = new List<RecordType>([SELECT Name,DeveloperName                                               
FROM RecordType WHERE SObjectType='Account']);

System.Debug('ccc'+lstRec);*/

for(RecordType  a : [Select Id, Name,DeveloperName FROM RecordType where SobjectType = 'Account']) 
{
    //below we are checking and adding only those record type names which are not equal to selected record's record type name.
    if(a.Id != acc.RecordTypeId){
        recTypeList.add(new selectOption(a.DeveloperName, a.DeveloperName ));
    }
}

}
}
| improve this answer | |
0

Without using the SOQL query on RecordType object you can create recordtype entries. You need to use Schema.Account.sObjectType.getDescribe().getRecordTypeInfosByName().values() to get RecordTypeInfo.

From RecordTypeInfo, you will get the name using getName() method.

Controller

public with sharing class AccountRecTypeChange_CLS {

    public Account acc {get;set;}
    public List<SelectOption> recTypeList {get;set;}
    public String strRecTypeName {get;set;}
    public AccountRecTypeChange_CLS(ApexPages.StandardController controller) 
    {
        this.acc = (Account)controller.getRecord(); 

        //new line of initialization inserted here.
         recTypeList = new List<SelectOption>();

        //retrieve Account recordtypes  
         for(Schema.RecordTypeInfo recInfo : Schema.Account.sObjectType.getDescribe().getRecordTypeInfosByName().values())
         {
                recTypeList.add(new selectOption(recInfo.getName(), recInfo.getName());

                //compare recordtypeid with the account record's recordtypeId
                if(recInfo.getRecordTypeId() == this.acc.RecordTypeId)
                {
                    strRecTypeName = recInfo.getName();
                }
         }
    }
}

Also, recInfo.getRecordTypeId() == this.acc.RecordTypeId will help you to assign selected recordtype.

Visualforce

<apex:selectList value="{!strRecTypeName}" size="1">
    <apex:selectOptions value="{!recTypeList}"/>
</apex:selectList>
| improve this answer | |
  • You can't get DeveloperName that way though. – Adrian Larson May 26 '17 at 17:13
  • that is true. we will get the label – Santanu Boral May 26 '17 at 17:19
  • But from the above code i'm facing error like " Expression of type Schema.RecordTypeInfo has no member named getRecordTypeId" while comparing @santanu boral – ram135868 May 30 '17 at 12:11
  • Sorry, my typo, updated the answer and I have run in my DE, it is working – Santanu Boral May 30 '17 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.