How i can access attachment id from this query..

List<Email__c> e=[SELECT email_body__c,(SELECT Id,Name FROM Attachments) FROM Email__C where id='a05i0000002R7iBAAS'];
for(email__c e1:e)

Getting error.. Invalid foreign key relationship Email__c.Attachments


Try this

 List<Email__c> e=[SELECT email_body__c,(SELECT Id,Name FROM Attachments) FROM Email__Cwhere id='a05i0000002R7iBAAS'];
for(email__c e1:e) {
List<Attachment> attList = e1.Attachments;
System.debug('@@@@Attachment list'+attList);

When traversing relationships incorrectly. Each relationship between objects ends in __r and you can verify their names in the Enterprise WSDL. Chances are though that it's Attachments__r, so your Apex likely should be:

for(email__c e1:e)
      for(Attachment a:e1.Attachments__r)
  • if Email__c contains multiple attachments, the above case will fail,am i rite ? – Venkatesh Aug 2 '13 at 12:11
  • You're right; I've updated my answer to show how to traverse the inner query. – Mike Chale Aug 2 '13 at 12:18
  • rite, i given same solution but not traversing through attachments but displaying all the attachments in debug logs – Venkatesh Aug 2 '13 at 12:32
  • Wouldn't you be traversing them to display them? What's your goal? You could also use the indexer, e1.Attachments__r[0].id, if you want a specific entry. – Mike Chale Aug 2 '13 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.