Alternative Title : How to optimize grouping each SObject records by Recordtypes
public static Id sgRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Singapore').getRecordTypeId();
public static Id myRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Malaysia').getRecordTypeId();
for(Account accObj : newAccountList) {
SWITCH on accObj.RecordTypeId {
when sgRecordTypeId {
sgAccountIdSet.add(accObj.Id);
}
when myRecordTypeId {
myAccountIdSet.add(accObj.Id);
}
...
}
When I do the above code I get this error : Id is not a valid switch expression type
So I tried changing it to SWITCH on String.valueOf(accObj.RecordTypeId) {
as mentioned here but get these errors:
1. null occurs as more than one when branch for this switch statement
2. Field must be an enum reference
Realised the error is because You can only use explicit string literals (no variables)
But got stuck here ...
Note : Don't want to use if/else statement because it will make the Apex PMD complain about the cyclomatic complexity