0

I am writing a batch job, and got stuck. I am trying to create a list 1, and then another list 2, but want to identify only the records in list 2 that match a field in list 1.

Here is an example of what I am trying to do. I think I must use a for loop to accomplish what I am looking for:

map<id,FEMA_DATA__c> setFemaDataId = new map<id,FEMA_DATA__c>([Select id, FEMA_ID__c From FEMA_DATA__c]);

case[] cafd = [Select id, FEMA_ID__c, AccountId From Case Where FEMA_ID__c = :femadatalist.FEMA_ID__c];
  • So you're trying to find cases that contain a FEMA_ID__c from FEMA_DATA__c? – sfdcfox Oct 9 '18 at 21:45
  • FEMA_ID__c is a text field that will contain an id. That Id will be passed onto the Case and the FEMA_DATA__c object. I want to find all Cases that have matching FEMA_ID__c from the FEMA_DATA__c. – Ivo Dimov Oct 10 '18 at 12:06
1

If FEMA_ID__c is a lookup field to some third object, you can simply use a subquery here:

SELECT Id, FEMA_ID__c, AccountId 
FROM Case
WHERE FEMA_ID__c IN (SELECT FEMA_ID__c FROM FEMA_DATA__c)

If FEMA_ID__c is not a lookup field but a string or some other type of data point, you can accumulate a Set of that type and use it in an IN clause. Below, assuming this field is a String:

Set<String> femaIds = new Set<String>();

for (FEMA_DATA__c fd : [Select id, FEMA_ID__c From FEMA_DATA__c]) {
    femaIds.add(fd.FEMA_ID__c);
}

case[] cafd = [Select id, FEMA_ID__c, AccountId From Case Where FEMA_ID__c IN :femaIds];
  • Thank you David. Yes FEMA_ID__c is a string field that would hold a unique ID passed from another system. I think this would work. Thank you !!! – Ivo Dimov Oct 10 '18 at 12:48

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.