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
Rafael.Martins.SantosRafael.Martins.Santos 

How get a Value of a field in a InnerJoin SOQL?

Hi,

I'm trying to get a child field with SOQL, but I not have success.

I must get the fields of Object Account and Object Product_Account (Child of Account).

I write A query like this:

LIST<Account> accounts = [SELECT Account.Name, (SELECT Product_Account__c.Status__c FROM Product_Account __r) FROM Account];
for(Account account : accounts){
 System.debug('Account Name: ' + account.Name + ', Product Name: '+ HERE I WANT GET THE VALUE FROM "Product_Account__c.Status__c" FIELD);
}

Is it possible to do?

Thanks
Rafael

 
Best Answer chosen by Rafael.Martins.Santos
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
LIST<Account> accounts = [SELECT Name, (SELECT Status__c FROM Product_Account__r) FROM Account];
for(Account account : accounts)
{
	
	system.debug('Account Name'+account.Name);
	List<Product_Account__c> lstProdAcc = account.Product_Account__r;
	for(Product_Account__c pa : lstProdAcc)
	{
		system.debug('--------->'+pa.Status__c);
		
	}
}

Let us know if this will help you
 

All Answers

Srinivas d 7Srinivas d 7
try this query
for (Account p : [SELECT Id, Name, (SELECT Id, Name, Status__c FROM Product_Accounts__r) FROM Account])
{
//process this
}
 
Rafael.Martins.SantosRafael.Martins.Santos
No this not work.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code.
LIST<Account> accounts = [SELECT Name, (SELECT Status__c FROM Product_Account__r) FROM Account];
for(Account account : accounts)
{
	
	system.debug('Account Name'+account.Name);
	List<Product_Account__c> lstProdAcc = account.Product_Account__r;
	for(Product_Account__c pa : lstProdAcc)
	{
		system.debug('--------->'+pa.Status__c);
		
	}
}

Let us know if this will help you
 
This was selected as the best answer
Rafael.Martins.SantosRafael.Martins.Santos
Hi Amit,

Thanks, it's work.