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
Ignacio de la torreIgnacio de la torre 

How to compare field values from different not related Objects in a SOQL query?

Hi all !
I'm trying to get a Map<String, Contact> with the returned values of this comparison:

I've got two Objects, Contact (Standard) and DFJSON (Custom). These two objects are NOT related.
I need to Select the Contact Objects where Contact.SSN__c = DFJSON.SSN__c and so on with some other fields but I'm not being able to do so sience there's no JOIN in SOQL.

Any idea on how to do this ?
 
VinayVinay (Salesforce Developers) 
Hi,

If your objects are not related, then you cannot query two objects in SOQL.
However, if you happen to need to query to do a full-text search, then SOSL can run on multiple objects.

Below are working examples of SOSL for your reference.

https://apexcoder.com/2016/02/22/how-to-use-sosl-query-to-search-in-multiple-salesforce-objects/
https://trailhead.salesforce.com/en/content/learn/modules/apex_database/apex_database_sosl
https://www.sfdcpoint.com/salesforce/sosl-example-in-salesforce/
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL.htm
http://www.infallibletechie.com/2012/07/example-for-soslsalesforce-object.html
http://www.sfdcpanda.com/dynamic-sosl-query-on-the-fly-in-apex/

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar
Ignacio de la torreIgnacio de la torre
Nothing to do with what I'm looking Vinay !
David Zhu 🔥David Zhu 🔥
Assume SSN__c is unique on both Contact and DFJSON, you may use the following code snippet as reference.
 
List<Contact> contacts = [select SSN__c,xxxx from Contact];
List<DFJSON__c> DFJSONs= [select SSN__c,xxxx from DFJSON__c];


Map<String,DFJSON__c> DFJSONMap= new Map<String,DFJSON__c>();

for (DFJSON__c c: DFJSONs)
{
    DFJSONMap.put(c.SSN__c,c);
}

for (Contact c: contacts)
{
    if (DFJSONMap.contansKey(c.SSN__c))
    {
         ........................
     }
}



 
Ignacio de la torreIgnacio de la torre

Thanks for your reply David !
That is what I need to do but inside of a SOQL query, exactly !

I can't do it the way you're suggesting because I could eventually have thousands of contacts and DFJSON's and I don't want to iterate over each one of them .... that's why I'm trying the query approach !

Any idea ?

 

David Zhu 🔥David Zhu 🔥
Then you can go with Apex batch job.
in batch Start method:
query = "select ssn__c,xxxxx from contact".


in execute method:
List<string> ssns = new list<string>();
for (conatct c : contacts)
{
ssns.add(c.ssn__c);
}

list<dfjson> =[select ssn__c,xxx form dfjson__c where ssn__c in : ssns];

.. then apply the code in my prevoius reply
Rick Smith 47Rick Smith 47
I facing the same problem and now I fixed the problem from the expert's replies. 

(https://cinemahdapkdownload.com)
Rick Smith 47Rick Smith 47

I facing the same problem and now I fixed the problem from the expert's replies. 

( https://cinemahdapkdownload.com )