Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
error

We made a wrong turn. Try again.

Hello,

I am getting a System null pointer because of the RecordTypeId.

Could someone help me understand why I can't insert the record.

Thanks

 

List<Sub_Step__c> subStepList = new List<Sub_Step__c>();

for(List<Template_Step__c> tStepList : hcJvsTempStepMap.values()) {

for(Template_Step__c tStep : tStepList ){

for(Template_Sub_Step__c temSubStepInst : tstep.Template_Sub_Steps__r){

subStepList.add(new Sub_Step__c(Name=temSubStepInst.Sub_Step_Master__r.Name,

RecordTypeId = Schema.SObjectType.Sub_Step__c.getRecordTypeInfosByName().get(temSubStepInst.Sub_Step_Master__r.Name).getRecordTypeId(),

Sub_Step_Number__c = temSubStepInst.Sub_Step_Number__c,

Step__c =tempStepIdvsStepMap.get(tStep.id).Id,

Template_Sub_Step__c = temSubStepInst.Id));

}

}

}

if(!subStepList.isEmpty())

insert subStepList;

1 answer
  1. Nov 17, 2021, 4:05 AM
    Hi Hermann,

    Because RecordType is returning null ,

    you can follow below code:

    Option 1: -

    Schema.SObjectType.SObjectAPIName.getRecordTypeInfosByName().get('record type label').getRecordTypeId();

    Schema.SObjectType.SObjectAPIName.getRecordTypeInfosByName().get('record type label').getName();

    Schema.SObjectType.SObjectAPIName.getRecordTypeInfosByName().get('record type label').getDeveloperName();

    OR

    Schema.SObjectType.SObjectAPIName.getRecordTypeInfosByDeveloperName().get('record type name').getRecordTypeId();

    Schema.SObjectType.SObjectAPIName.getRecordTypeInfosByDeveloperName().get('record type name').getName();

    Schema.SObjectType.SObjectAPIName.getRecordTypeInfosByDeveloperName().get('record type name').getDeveloperName();

    OR

    Schema.SObjectType.SObjectAPIName.getRecordTypeInfosById().get('record type Id').getRecordTypeId();

    Schema.SObjectType.SObjectAPIName.getRecordTypeInfosById().get('record type Id').getName();

    Schema.SObjectType.SObjectAPIName.getRecordTypeInfosById().get('record type Id').getDeveloperName()

    Option 2: -

    Sample Class:-

     

    public class ObjectUtility {

    public static Map<String,Schema.RecordTypeInfo> recordTypeInfoMap = new Map<String,Schema.RecordTypeInfo>();

    public static Id getObjectRecordTypeId(String sObjectName, String recordTypeName) {

    sObject obj;

    Schema.SObjectType sObjectType = Schema.getGlobalDescribe().get(sObjectName);

    if(sObjectType != null) {

    obj = sObjectType.newsObject();

    Schema.DescribeSObjectResult sObjDesc = obj.getSObjectType().getDescribe();

    if(sObjDesc != null) {

    recordTypeInfoMap = sObjDesc.getRecordTypeInfosByName();

    }

    if(!recordTypeInfoMap.containsKey(recordTypeName)) {

    throw new RecordTypeException('Record type "'+ recordTypeName +'" does not exist.');

    } else {

    return recordTypeInfoMap.get(recordTypeName).getRecordTypeId();

    }

    }

    return null;

    }

    public class RecordTypeException extends Exception{}

    }

    ID RecordTypeId = ObjectUtility.getObjectRecordTypeId(‘Account’ , ‘PersonAccount’);

    if you need any assistanse, Please let me know!!

    Kindly mark my solution as the best answer if it helps you.

    Thanks

    Mukesh 

     
0/9000