function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Melissa BunchMelissa Bunch 

Apex Class - Get records by record type that also reference a field value on found records

I don't write code, so bear with me.

I have a portion of an Apex Class that pulls a list of records based on record type for Cases. However, I need to add a criteria here that then looks at the received records and only includes the records where a certain field is filled in.

Current code:


List<RecordType> residentialRecordTypes = [SELECT Id
                                           FROM RecordType
                                           WHERE (DeveloperName = 'Residential' OR DeveloperName = 'Carpet' OR DeveloperName = 'Manufacturing_Distribution' OR DeveloperName = 'Residential_Service_Claims' OR DeveloperName = 'Mannington_Rubber')
                                           AND SobjectType = 'Case'
                                           AND Id in: casesByRecordTypeId.keySet()
                                           LIMIT 1];


So I essentially need to add something like:

AND Case.Division__c LIKE 'Residential'

Any idea how I would do this?

Thank you!

Melissa
AbhinavAbhinav (Salesforce Developers) 
The criteria you are suggesting is for case however its not pulling data for cases its for recordtype.
 
Melissa BunchMelissa Bunch
Thank you @Abhinav.

I looked at the code again and found the area where the initial criteria for List <Case> (not record types) were being pulled from and I added my criteria there instead.
After some testing, it seems to be working.

Thanks!

public void determineRoundRobinEligibility(Map<Id,Case> oldMap, List<Case> caseRecordsToUpdate){
        List<Case> casesToAssign = new List<Case>();

        for (Case newCase: caseRecordsToUpdate){
            Case oldCase = oldMap.get(newCase.Id);

            if (newCase.Status == 'Claim Authorized' && oldCase.Status != 'Claim Authorized' && newCase.Division__c.CONTAINS ('Residential') && (newCase.RecordType.DeveloperName == 'Residential' || newCase.RecordType.DeveloperName == 'Carpet' || newCase.RecordType.DeveloperName == 'Manufacturing_Distribution' || newCase.RecordType.DeveloperName == 'Residential_Service_Claims' || newCase.RecordType.DeveloperName == 'Mannington_Rubber')){
                casesToAssign.add(newCase);
            }