Friday, June 20, 2014

How to get records from inner SOQL query

Inner SOQL query are great way to get related child records when querying parent records. In back end its just file "Join" query so that results will be displayed encapsulated by parent records. I came up with this query when I need to get all attachments of a account and display it on the visualforce page as a download link.

List<Account> vAccLst = [SELECT Id, Name, Type, (SELECT Id,ContentType from Attachments) from Account where Id IN: vSetAcc];

This will return the result in some what this form :
You can use the attachment as mentioned below:

  • String IdAttach = vAccLst[0].Attachments[0].Id;

or a more robust approach is :

  • Set<String> attachId = new Set<String>();
  • for(Account acc: vAccLst){
  • for(Attachments a : acc.Attachments){
  •      attachId.add(a.Id);
  • }
  • }

No comments:

Post a Comment