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
sroberts_MEsroberts_ME 

using my apex query results

Hi all,

 

I've used the following query to get my data.

 

list<Account> accountList =  [SELECT Id, Name, (SELECT Id, Name, Product__r.Name FROM Assets__r) FROM Account];

 

Account is the parent to Assets in a master detail relationship. Because of this I used the subquery to get information from assets. The Product is the parent in the Asset/product relationship, so I used the dot notation in the subquery.

 

The question now is how do I access this info.

 

for(Account Acc : accountList)

{

if (Acc.Asset__r.Product__r.Name == 'something')

{

//do stuff

}

}

 

The above does not work because Acc.Asset__r is not a valid relationship because Account is the parent. If this is the case how can I access it?

 

Thanks, 

sam

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

If you were using the standard Asset and Product objects in SF, this would let you access the Product Name. Assets is a 1: MANY relationship to Account, so you have to loop through a list to access the assets for each account

 

List<Account> a  = [Select Account_ID__c, (Select Id, Product2.Name, Name From Assets) From Account];

for (Account alist: a) {

for (Asset ast: alist.assets) {
system.debug(ast.Product2.Name);
}

} 

 

All Answers

BritishBoyinDCBritishBoyinDC

If you were using the standard Asset and Product objects in SF, this would let you access the Product Name. Assets is a 1: MANY relationship to Account, so you have to loop through a list to access the assets for each account

 

List<Account> a  = [Select Account_ID__c, (Select Id, Product2.Name, Name From Assets) From Account];

for (Account alist: a) {

for (Asset ast: alist.assets) {
system.debug(ast.Product2.Name);
}

} 

 

This was selected as the best answer
sroberts_MEsroberts_ME

seeing that isnt the question I asked, it is safe to assume that the standard objects are not good enough in this case

BritishBoyinDCBritishBoyinDC

The principle is the same - even if your asset object is a custom object, it is still a detail object, with a lookup to an object called Product - you would still access it this way, but you might need to use something like Eclipse or SOQL Explorer to work out the actual SOQL relationship names if you can't work them out via the UI. 

sroberts_MEsroberts_ME

The issue is that I can not use Acc.asset__c or any slight variation of that because the account object does not have a field for assets, the asset object contains the master detail relationship.

BritishBoyinDCBritishBoyinDC

Just to confirm - you have a custom object called Assets__c which is a detail object of Account i.e. each Account can have many assets but an asset can only have one Account? Product__c is a lookup to the standard Product object from the custom object called Assets__c?

 

So when you query for accounts with assets, you get back a list of Accounts, and for EACH account, you will also get back a list of Assets - even if there is only asset for the Account, it is still a List of Assets related to the Account.

 

If so, the code above shows how you have to create a double loop - the first loop is to loop through the accounts returned by the query. Then, for each account, you create a second loop - which is a loop of the assets for the account currently in context in the loop. At that point, you can the access each asset, and it's related product.

 

So in your example, this won't work:

if (Acc.Asset__r.Product__r.Name == 'something')

 

because Acc.Asset__r is a list of assets for that account, not an individual record.

 

To see it work as you've coded it above, you could try something like this if the account has at least one Asset, though not 100% about the syntax:

Acc.Assets__r[0].Product__r.Name == 'something'

 

If you have a developer account, you can create some sample Account/Product/Asset records and try my code above - you'll see how the two loops work together and enable you to access the Product Name for an asset for a particular asset 

 

 



 

 

 

 

 

sroberts_MEsroberts_ME

ugggh, sooooorrrryyy. really dumb mistake. My object was asset__c, but the relationship was assetS__r, all set now.