3

I'm unable to update record type of a custom object.

Other fields are getting updated but not Record Type

Please help

Here is my code,

trigger UpdateRecordType on ZI_Quote__c (before Update) {

    List<ZI_Quote__c> ZIQuotes = new List<ZI_Quote__c>();
    Map<String,ID> RecordTypeMAP = new Map<String, Id>();
    for (RecordType RecType: [Select Id, DeveloperName from RecordType where sObjectType ='ZI_Quote__c']){
        RecordTypeMAP.put(RecType.DeveloperName, RecType.id);
    }

    For(ZI_Quote__c ZIQ : trigger.new){

        IF (ZIQ.Hours_Remaining__c <= 0 && ZIQ.Status__c == 'Received From Supplier'){
            ZIQ.RecordTypeID = RecordTypeMAP.get('Remaining hr');
            ZIQuotes.add(ZIQ);
        }
        Else IF(ZIQ.Hours_Remaining__c <= 0 && ZIQ.Status__c == 'Submitted To Supplier'){
            ZIQ.Buyer_Name__c= 'TEST';
            ZIQ.RecordTypeid = RecordTypeMAP.get('Before 48 hr remaining time');
            ZIQuotes.add(ZIQ);
        }
    }
}
2

The problem is that you are using wrong names to retrieve the Id of the record type.

I would assume that the DeveloperName for Before 48 hr remaining time would be Before_48_hr_remaining_time.

Note that DeveloperName can not have any spaces in it, so you are most likely using just a normal Name field of the RecordType, and not DeveloperName

And because you're tying to get something from a Map which doesn't have a valid key, the Map will just return null, so your RecordTypeID will always be null

2

You are adding the DeveloperName of the record types as the key to your map.

but when you are retrieving the value from the map, you are using the record type label instead of the Developer Name.

usually the developer name will have underscores instead of spaces.

so use the developer names as keys when retrieving the record type id from the map.

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.