You need to sign in to do that
Don't have an account?
farah 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();
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();
2-how to do that
- From Setup, enter Profiles in the Quick Find box, then select Profiles.
- Select your profile. The record types available for that profile are listed in the Record Type Settings section.
- Click Edit next to the appropriate type of record.
- Select a record type from the Available Record Types list and add it to the Selected Record Types list.
- 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.
- From Default, choose a default record type.
- Click Save.
https://help.salesforce.com/articleView?id=admin_recordtype.htm&type=5can you call like this
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*
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