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
Matt_FMatt_F 

Need help with SOQL query

I am creating a VF page to show all accounts and contacts (name, email address) that have an asset that is selected from a picklist.

 

I am having a problem creating the query.  I need to query Accounts, Contacts and Assets.  I can't do it as a Parent - Child because I can only do 1 child.

 

My goal is to have a query that shows all the accounts, and all of the contacts associated with the accounts for a given asset.

 

Any help that I get will be greatly appreciated.

 

Thanks

petergascoynepetergascoyne

Hi,

 

I don't think you'll be able to gain all information in one query, but below show how you could use two:

 

String assetId = '';
Asset theAsset = [Select AccountId From Asset Where Id =: assetId];

List<Account> assetAccounts = [Select Id,(Select Name, Email From Contacts) From Account Where Id =: theAsset.id];

 

All the best,

 

Peter

Matt_FMatt_F

Thanks Peter.  I tried this query and I think that it has what I am looking for.  Now I am trying to figure out how to incorporate it into a VF page.  I want to have a picklist to populate the query for the asset name (which is IFPS in the example I attached).

 

SELECT contact.account.name, firstname, lastname, email, mailingstreet, mailingcity,mailingstate, mailingpostalcode
FROM Contact	
WHERE AccountId IN (SELECT AccountId FROM Asset WHERE Name = ‘IFPS’)

 Any hints or tips are gratefully accepted.

 

Thanks