+ Start a Discussion
Lukas van RooyenLukas van Rooyen 

How to compare a result gotten from one query to the ID of another in where clause

I need to use Contact__c that I got in pol info query to compare it to the id of the cnt info query to get that info but what I have is obviously incorrect. Anybody have idea to fix that? Thank you.    

InputId = ApexPages.currentPage().getParameters().get('InputId');      

 PolInfo=[Select Id,Name,Policy_Number__c, Contact__c From PolContract__c where Id=:InputId];  
     
CntInfo=[Select Id,Name, MailingAddress from Contact where Id=:PolInfo.Contact__c];
Best Answer chosen by Lukas van Rooyen
Jakub Kołodziej 7Jakub Kołodziej 7
You want to create nested query, in link below you have all informations.
https://blog.jeffdouglas.com/2010/02/22/soql-how-i-query-with-thee-let-me-count-the-ways/

Try below query:
InputId = ApexPages.currentPage().getParameters().get('InputId');      
List<Contact> records = [Select Id,Name, MailingAddress from Contact c where c.Id in (Select Contact__c From PolContract__c where Id=:InputId )];