You need to sign in to do that
Don't have an account?
Patrick Conner
Multiple Object Query with SOQL
I'm trying to create an SOQL Query for the controller of a custom VisualForce page. Is there any way to run a single SOQL query that can search fields in two different non-related objects? I'm aware that this can be done in SOSL, but there are specific reasons SOQL is required for this particular function (please no SOSL responses).
Basically I'd like to combine the following (examples):
SELECT Name, Email, Phone FROM Contact WHERE Phone != null AND PhoneSearch1__c LIKE '%0149%' order by Name asc limit 75
SELECT Name, Email__c, Phone FROM Account WHERE Phone != null and PhoneSearch1__c LIKE '%0149%' order by Name asc limit 75
This might could be done by assigning the queries as variables? I'm not sure. Any help would be greatly appreciated! Thanks!
Basically I'd like to combine the following (examples):
SELECT Name, Email, Phone FROM Contact WHERE Phone != null AND PhoneSearch1__c LIKE '%0149%' order by Name asc limit 75
SELECT Name, Email__c, Phone FROM Account WHERE Phone != null and PhoneSearch1__c LIKE '%0149%' order by Name asc limit 75
This might could be done by assigning the queries as variables? I'm not sure. Any help would be greatly appreciated! Thanks!
If Account and contact have realtionship record then you can use inner query with semi joins.
But in terms of minimizing the duplicate code, you can use dynamic soql query. If you are sure that your fields are where clause criteria is going to be the same, you can have a variable for refering the object name. Make sure each query will count as separate.