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
Aratz GuerraAratz Guerra 

need query in SOQL

Hello, I have this query:

SELECT b.Outstanding_Amount__c
FROM Billing__C b, Contract c
WHERE b.Contract_Number__C = c.ContractNumber
AND  c.AccountId =: cuenta.Id 

(cuenta.Id is the value that I pass)

But i need to do it in SOQL, could anybody help me pease?
 
Best Answer chosen by Aratz Guerra
devedeve
Hi Aratz,

This is not possible in one soql. You can try this bu using

List<Contract> contractLst = [SELECT Id, ContractNumber FROM Contract WHERE c.AccountId =: cuenta.Id];
List<String> contractNumbers = new List<String>();
for(Contract contract : contractLst) {
    contractNumbers.add(contract.ContractNumber);
}
List<Billing__C> billingLst = [SELECT b.Outstanding_Amount__c FROM Billing__C b WHERE b.Contract_Number__C IN: contractNumbers];

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Can you please explain your issue in detail ?
Aratz GuerraAratz Guerra
Sorry I haven´t explain myself

What I mean is that I want to rewrite the originsl query to SOQL.

I´ve tried something like this:

myList = [select Outstanding_Amount__c FROM Billing__C WHERE Contract_Number__C IN (select ContractNumber FROM Contract WHERE AccountId =: cuenta.Id)];

but it doesn´t work.... any clue?
devedeve
Hi Aratz,

This is not possible in one soql. You can try this bu using

List<Contract> contractLst = [SELECT Id, ContractNumber FROM Contract WHERE c.AccountId =: cuenta.Id];
List<String> contractNumbers = new List<String>();
for(Contract contract : contractLst) {
    contractNumbers.add(contract.ContractNumber);
}
List<Billing__C> billingLst = [SELECT b.Outstanding_Amount__c FROM Billing__C b WHERE b.Contract_Number__C IN: contractNumbers];
This was selected as the best answer
Aratz GuerraAratz Guerra
Thank you deve, it works :)