+ Start a Discussion
Andrew TelfordAndrew Telford 

SOQL: Don't Understand relationship 'attachment' in FROM part of your Query call

I am getting the following error whe I attempt to execute a query.
 
Line: 4, Column: 14
Didn't understand relationship 'attachment' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
The query (being executed via the debug in developer console
PUBLIC STRING strTemplate_Initial;
strTemplate_Initial = 'TEST EMAIL';

PUBLIC LIST <ci_mailout__c> objMailout;

objMailout = [SELECT m.Id, m.contact__c, contact__r.Name, m.recipient_first_name__c, m.recipient_last_name__c, caps__r.AccountID__c, m.file_password__c, m.email_address__c, (SELECT ID, Name, ContentType, body FROM attachment) FROM ci_mailout__c m WHERE Sent__c = FALSE AND Mailout_name__c = :strTemplate_Initial ORDER BY Policy_count__C Desc LIMIT 100];

SYSTEM.debug('Size of Mailout: ' + objMailout.size());

The code I originally had was two seperate queries on for the mailout and one for attachements which worked, however, it starts to hit the limit for SOQL statements and so I was thinking I could join the two statements to reduce the calls.

Original SOQL (in isolation)
objMailout = [SELECT Id, contact__c, contact__r.Name, recipient_first_name__c, recipient_last_name__c, caps__r.AccountID__c, file_password__c, email_address__c FROM ci_mailout__c WHERE Sent__c = FALSE AND Mailout_name__c = :strTemplate_Initial ORDER BY Policy_count__C Desc LIMIT 50];

objAttachment = [SELECT ID, Name, ContentType, body FROM attachment WHERE ParentId = :thisMailOut.Id];

Thanks for your help ...



 
Best Answer chosen by Andrew Telford
@Karanraj@Karanraj
When you are querying the child objects in the parent object query use the plural name for the standard objects. Try the below updated query statement.
objMailout = [SELECT m.Id, m.contact__c, contact__r.Name, m.recipient_first_name__c, m.recipient_last_name__c, caps__r.AccountID__c, m.file_password__c, m.email_address__c, (SELECT ID, Name, ContentType, body FROM attachments) FROM ci_mailout__c m WHERE Sent__c = FALSE AND Mailout_name__c = :strTemplate_Initial ORDER BY Policy_count__C Desc LIMIT 100];

 

All Answers

@Karanraj@Karanraj
When you are querying the child objects in the parent object query use the plural name for the standard objects. Try the below updated query statement.
objMailout = [SELECT m.Id, m.contact__c, contact__r.Name, m.recipient_first_name__c, m.recipient_last_name__c, caps__r.AccountID__c, m.file_password__c, m.email_address__c, (SELECT ID, Name, ContentType, body FROM attachments) FROM ci_mailout__c m WHERE Sent__c = FALSE AND Mailout_name__c = :strTemplate_Initial ORDER BY Policy_count__C Desc LIMIT 100];

 
This was selected as the best answer
Andrew TelfordAndrew Telford
Thanks for the response Karanrajs.

While the query now works, it doesn't let me select the Body as this is stored in Binary when you use joined queries.