[SalesForce] Fetching Record Type Name / Id values from Custom Settings – best practise

I need to check that the Record Type of the record whose change caused my trigger to fire is listed in a Custom Setting WEID__c.

Until now I have been been using

trigger WE_MRFv2 on Opportunity (after insert, after update) {

    List<WEID__c> weids = WEID__c.getall().values();
    Set<Id> validRecordTypeIds = new Set<Id>();

    // add Opportunity Record Type Ids from Custom Setting to list of valid Ids      
    for(WEID__c weid : weids) {
        try {
            validRecordTypeIds.add(weid.WEOppId__c);
        } catch (System.StringException e) {
            System.debug(
                System.LoggingLevel.ERROR,
                'Invalid Record Type Id ' + weid.WEOppId__c);
        }
    }

If(Trigger.isInsert){
        for(Opportunity o : Trigger.New){
            if(validRecordTypeIds.contains(o.RecordTypeId))
            {
            //do something
            }
        }
    }

But it's been suggested that I change this to

trigger WE_MRFv2 on Opportunity (after insert, after update) {

    WEID__c weid = WEID__C.getAll().values();

    If(Trigger.isInsert){
        for(Opportunity o : Trigger.New){
            if(weid.get(o.RecordType.Name) != null)
            {
            //do something
            }
        }
    }

As the Record Type name is more stable (when copying code between Sandboxes & Production) and I can then avoid hard coding Record Type Ids when creating / referencing Custom Settings in my Test Classes.

UPDATE

In the end the below portion of my question was irrelevant since even if I could fetch the Record Type Name from the Custom Setting, I can't check that against the Opportunity Record Type Name.

The only problem is that I have several fields in the Custom Setting containing the same value and I'd like to avoid having to create a new Custom Setting each time I need a new list of Record Types (which are currently just stored in a new field for the same Custom Setting).

So I see the following error

System.QueryException: List has more than 1 row for assignment to SObject

Supposedly at line WEID__c weid = WEID__C.getAll().values();. But I'm guessing that the issue actually occurs when I try to fetch the values from weid at line if(weid.get(o.RecordTypeId) != null)?

The 'cost' of fetching the values from the Custom Setting using an SOQL query outweighs the benefit of switching to using Record Type Names, from my point of view.

Is there alternative way to fetch the Custom Setting values which I need (contained in field WEOppId__c), to query later in the Trigger?

Best Answer

A problem with switching to the name is that in triggers only the immediate fields of objects are available. So RecordTypeId is available but the related field that has the record type name RecordType.Name is not without performing an extra query.

ID values in sandboxes are the same as in production because a sandbox is a clone of production.

So I suggest that you do not make this change.

Related Topic