0

This batch class is created to set a recordtype to the records having NULL recordtypes. But it throws an error

FATAL_ERROR System.NullPointerException: Attempt to de-reference a null object in line 17.

When looking in the debug logs, the Recordtype Id is assigned to the contact but the error is when assigning the name. So the Recordtype the standard contact page still remains NULL. Any idea what's wrong with my code? Thanks.

global class ContactBatch implements Database.Batchable<SObject> {
    global String query;
    global Database.QueryLocator start(Database.BatchableContext BC) {
        query = 'SELECT RecordTypeId, RecordType.name, Email, Account.Name FROM Contact WHERE RecordTypeId = null';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<SObject> batchRecords) {

        Map<String, Schema.RecordTypeInfo> CON_RECORD_TYPES = Schema.SObjectType.Contact.getRecordTypeInfosByName();
        String CON_RECORD_TYPES_ID = CON_RECORD_TYPES.get('PRIMARY DEALER').getRecordTypeId();
        String CON_RECORD_TYPES_NAME = CON_RECORD_TYPES.get('PRIMARY DEALER').getName();

        for(SObject c : BatchRecords) {
            Contact con = (Contact)c;
            con.RecordTypeId = CON_RECORD_TYPES_ID;
            if(con.RecordType.Name == null) {
                con.RecordType.Name = CON_RECORD_TYPES_NAME; //FATAL_ERROR LINE
            }
        }
    }
    global void finish(Database.BatchableContext BC) {
    }
}
2
  • You do not assign the Name. The ID is all you need to assign. The Name is determined by the relationship to the Recordtype record based on the RecordTypeID.
    – Eric
    Nov 22 '14 at 0:52
  • @Eric That was my first attempt. There was no error and the batch was executed successfully. In the debug logs, I can view that the recordtypeId has been assigned to the contact. But when I query for the contact after the batch, the recordTypeId and the recordType.Name remains NULL. That' s why I tried to assign the name too.
    – Seesh
    Nov 22 '14 at 4:16
1

You shouldn't be trying to set the Record Type Name. RecordType is a related object...so when you set the Id, the name is set for you.

Just comment out these lines and it should be fine:

if(con.RecordType.Name == null) {
                con.RecordType.Name = CON_RECORD_TYPES_NAME; //FATAL_ERROR LINE
   }
2
  • That was my first attempt. There was no error and the batch was executed successfully. In the debug logs, I can view that the recordtypeId has been assigned to the contact. But when I query for the contact after the batch, the recordTypeId and the recordType.Name remains NULL. That' s why I tried to assign the name too.
    – Seesh
    Nov 22 '14 at 4:15
  • I've removed these line and appended 'update con;'. Thank you @BritishBoyinDC
    – Seesh
    Nov 22 '14 at 19:26
2

First of all, Eric and BritishBoyInDC are 100% correct. You always and only assign to con.RecordTypeId. RecordType.name is a relationship to the RecordType table

I see two issues:

  1. Your code returns the describe info into variable CON_RECORD_TYPES but you reference a different variable in the following line - contact_record_types -- could that be the problem? If a typo, please edit your post.
  2. Your batch execute() method never does a DML Update. So, whatever recordTypeId you assign will never be saved; Batch execute does not work like a before trigger where changes to Trigger.new are implicitly saved to the database when the trigger completes; in bathc execute, you have to explicitly do the DML.
1
  • Thanks @crop1645 Yes. I completely agree with them, I thought it might be some permission issue that is not allowing the record to update. Now I explicitly performed the DML (Added 'Update con;' to the execute block) and is working perfect. P.S. Corrected the typo in the post.
    – Seesh
    Nov 22 '14 at 19:24

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.