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
Sue Irvine 16Sue Irvine 16 

soql select from list when not in object

Hi all, I have a REST API endpoint set-up to receive a list of Id's (external ID on the Contact) for processing. I would like to be able to return a list of external ID's in this list, that don't exist in our Contacts. Although we currently have less that 50K Contacts, I know it won't be that way for much longer. This is what I'm doing currently:
List<String> badEmplids = new List<String>();
Set<Contact> allContacts = new Set<Contact>([select Cornell_id__c from Contact]);
Set<String> allConString = new Set<String>();
for (contact c : allContacts){
  allConString.add(c.cornell_id__c);
}
for (string s : emplidIds){
    if (! allConString.contains(s)){
        badEmplids.add(s);
     }
}
Am I missing something obvious? Ideally I would want to select from the LIST where the external id is not in the Contact.
Ravi Dutt SharmaRavi Dutt Sharma
Why not just do this?
[SELECT Id FROM Contact WHERE  Cornell_id__c NOT IN emplidIds]
Sue Irvine 16Sue Irvine 16
Because the emplidIds list is just a selective list of Contacts (maybe a thousand or two at most), not all of them. I want the ones in the List that aren't in Contacts. (
Narender Singh(Nads)Narender Singh(Nads)
Hi,
The problem is your 'badEmplids' list is empty. So there is no value to iterate on for the FOR loop.
What you are saying is not possible because you gotta have a list of external IDs to compare from right?
You can't compare an empty list.

You have to have a list of contaning all the external IDs regardless of it is present in your contacts.

If you don't have that option then what you can do is: 
Return the list of external IDs present in your contacts. And configure your API in such way that the logic is not performed for the IDs present in the returned list.
 
Sue Irvine 16Sue Irvine 16
Sorry for any confusion - the emplids list is populated, that code is just not included in my snippet.

Unfortunately I cannot create a list of all the external ID's in my Contacts as it would exceed the Governor limit of 50K rows in a SOQL query.

But thanks anyway for trying to help!
Narender Singh(Nads)Narender Singh(Nads)
Ohh, if that's the case then your code should work as expected. I don't see the any problem. Can you tell me what is the problem you getting in this code?
 
Sue Irvine 16Sue Irvine 16
The code is working fine right now - I'm concerned that when we have more than 50K Contact records that it will break because of the governor limit.

This code runs when someone uploads a list of external ID's via a CSV file, or from a JSON request. It is likely that someone will have a typo and send one or more invalid external ids. When that happens I'd like to return a list of the invalid entries so they know to correct those.
Narender Singh(Nads)Narender Singh(Nads)
In that case you have to resort to batch apex. There is no other option.
Sue Irvine 16Sue Irvine 16
OK, just wanted to make sure I wasn't missing something obvious. Thanks!!
Narender Singh(Nads)Narender Singh(Nads)
You're welcome!