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
DesaiDesai 

SOQL query on two unrelated objects

Hi,
We have two objects, one cusotm and and one OOTB and both are not related but they have one field which will have same values.

How can we build a SOQL query for this ? Is this even possible ?

Reards,
Desai
Ajay K DubediAjay K Dubedi
Hi Desai,
Try the following code:
public class TestStringSame {
    public static void check() {
      
        List<Account> accList = new List<Account>();
        Set<String> uniqueFieldSet = new Set<String>();
        List<OtherObject__c> OtherObjectList = new List<OtherObject__c>();
        
        accList = [SELECT Id, uniqueField__c FROM Account WHERE uniqueField__c != null LIMIT 10000];
        
        for(Account a : accList) {
            uniqueFieldSet.add(a.uniqueField__c);
        }
        if(uniqueFieldSet.size() > 0) {
            OtherObjectList = [SELECT Id, uniqueField__c FROM OtherObject__c WHERE uniqueField__c In: uniqueFieldSet LIMIT 10000];
        }
        system.debug('----' + courseList);
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
DesaiDesai
problem here is count of records from 2nd object is more than 50000.