+ Start a Discussion
farah sheriffarah sherif 

get record type id by name

i have this error
System.NullPointerException: Attempt to de-reference a null object
       
with this line in the code but it used to work before 
acc.RecordTypeId=Schema.SObjectType.Account.getRecordTypeInfosByName().get('Parent').getRecordTypeId();
 
Raj VakatiRaj Vakati
Do you have a record type with the name Parent ?? The code looks good for me and can you check  below 
  1. Check record type with the name "Parent" is there on the account or not 
  2. Check the profile access to the record type
Schema.SObjectType.Account.getRecordTypeInfosByName().get('Parent').getRecordTypeId();

 
farah sheriffarah sherif
1- yes
2-how to do that
Raj VakatiRaj Vakati
  1. From Setup, enter Profiles in the Quick Find box, then select Profiles.
  2. Select your  profile. The record types available for that profile are listed in the Record Type Settings section.
  3. Click Edit next to the appropriate type of record.
  4. Select a record type from the Available Record Types list and add it to the Selected Record Types list.
  5. Master is a system-generated record type that's used when a record has no custom record type associated with it. When you assign Master, users can't set a record type to a record, such as during record creation. All other record types are custom record types.
  6. From Default, choose a default record type.
  7. Click Save.
https://help.salesforce.com/articleView?id=admin_recordtype.htm&type=5
Raj VakatiRaj Vakati
Can u share the screenshoot of record type 
farah sheriffarah sherif
I opened my profile but I can’t find what you said
farah sheriffarah sherif
User-added image
Raj VakatiRaj Vakati
Are you using it in test class .. 

can you call like this
 
RecordType accRecTypeId = [Select id from RecordType where sObjectType = 'Account' and DeveloperName ='Parent' ];
acc.RecordTypeId = accRecTypeId .Id

 
Ramesh DepaiahRamesh Depaiah
Hi Farah,
Create below utility class, and use this line to get your recordtypeid

acc.RecordTypeId = RecordTypeHelper.getRecordTypeId('Account','Parent')


Utility Class: this class help to get recordtypeid by name and recordtypename by Id

public class RecordTypeHelper {   
  // internal static variables and methods
  private static Map<String, RecordType> m_rt = null;
  private static void fillRecordTypeMap() {
    m_rt = new Map<String, RecordType>();
        RecordType[] rtList = [Select Id, sObjectType, Name From RecordType];
        for(RecordType rt : rtList) {
          m_rt.put(rt.sObjectType + '|' + rt.Name, rt);
          m_rt.put(rt.Id, rt);
        }
  }

  private static RecordType getRecordType(String sobject_type, String recordtype_name) {
       if(m_rt==null || m_rt.size()==0) 
          fillRecordTypeMap();
        RecordType rt = m_rt.get(sobject_type + '|' + recordtype_name);
        return (rt!=null?rt:null);
    }
 
  // public methods
    public static Id getRecordTypeId(String sobject_type, String recordtype_name) 
    {
       RecordType rt = getRecordType(sobject_type, recordtype_name);
       return (rt!=null?rt.Id:null);
    }
 
    public static String getRecordTypeName(Id recordtype_id) {
      if(m_rt==null || m_rt.size()==0)
        fillRecordTypeMap();
      RecordType rt = m_rt.get(recordtype_id);
      return (rt!=null?rt.Name:null);
    }


Thanks
Ramesh

*Please mark this as best answer, so it helps others as well*
 
Deepali KulshresthaDeepali Kulshrestha
Hi Farah,

You should check the profile access of the record type.
NullPointerException de-reference a null object in Apex code trigger.
This error is caused by a line of code that is trying to use an object
that has not been instantiated or an object's attribute that has not
been initialized. NOTE: If the field Site was left empty, it will
generate the error message as well.
Check the profile access of the record type.


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha