You need to sign in to do that
Don't have an account?
Gaby Frydman
SOQL through 3 objects in child-parent relationship
Hi All,
I am trying to obtain all attachments from a Contact that has a record on Application__c which belongs to an object called Job__c.
The Application__c is in many-to-many (lookup) with Contact and master-detail with Job__c
The idea is that If I have the Id of the Job__c, to loop over it's applicaitons and obtain the attachemnts from the contact
I have currently the following code which does the job if i have the contact id :
Since I cannot have more than one level query in child-parent relationship, what can do the job?
I am trying to obtain all attachments from a Contact that has a record on Application__c which belongs to an object called Job__c.
The Application__c is in many-to-many (lookup) with Contact and master-detail with Job__c
The idea is that If I have the Id of the Job__c, to loop over it's applicaitons and obtain the attachemnts from the contact
I have currently the following code which does the job if i have the contact id :
Id jobId = 'a0K24000000QneY'; list<Attachment> attachments = [SELECT Id, Name, Body, ContentType FROM Attachment WHERE ParentId IN (SELECT Id from Contact WHERE id =: jobId)]; Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {'secret@secret.com'}; email.setToAddresses(toAddresses); email.setSubject('Foo'); email.setPlainTextBody('Bar'); List<Messaging.EmailFileAttachment> emailAttachments = new List<Messaging.EmailFileAttachment>(); for(Attachment att : attachments) { Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName(att.Name); efa.setBody(att.Body); efa.setContentType(att.ContentType); emailAttachments.add(efa); } email.setFileAttachments(emailAttachments); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
Since I cannot have more than one level query in child-parent relationship, what can do the job?
Could you please help me understand few things
1 --- does application has a lookup of contact or contact has a lookup of application?
2 -- Is Job the master object or applicatione?
3 -- Do you have the set of id's for the Job object in your code?
Thanks
Nitin
1- Application has lookup to contact
2 - Job is master object od application
3- Yes, I do - I will be running it in the apex code console in the developer console - I will set the JobId's manually in the
The lookup from Application to contact is called Candidate_Contact__c
for(Attachment att : attachments) as I havent properly written anything in the variable.
What am I doing wrong?
Please try this SOQL,
list<Attachment> atts = [select Id from Attachment where ParentId In (Select Candidate_Contact__c from Application__c where Job__c = :jobId)];
Please let me know if there is any issue.I hope this solves your problem .
Thanks
NItin
only aggregate expressions use field aliasing
Use this code,
Id jobId = 'a0K24000000QneY';
List<Application__c> existingApplications = new List<Application__c>();
Job__c jobOrder;
List<Id> contactIds = new list<Id>();
list<Attachment> attachments = new list<Attachment>();
if (jobOrder == null) {
existingApplications = [SELECT Id, Candidate_Contact__c FROM Application__c WHERE Job__c =: jobId];
for (Application__c appl : existingApplications) {
contactIds.add(appl.Candidate_Contact__c);
}
attachments = [SELECT Id, Name, Body, ContentType FROM Attachment WHere ParentId in :contactIds ];
}
}
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'secret@secret.com'};
email.setToAddresses(toAddresses);
email.setSubject('Foo');
email.setPlainTextBody('Bar');
List<Messaging.EmailFileAttachment> emailAttachments = new List<Messaging.EmailFileAttachment>();
for(Attachment att : attachments) {
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(att.Name);
efa.setBody(att.Body);
efa.setContentType(att.ContentType);
emailAttachments.add(efa);
}
email.setFileAttachments(emailAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
Id jobId = 'a0K24000000QneY';
List<Application__c> existingApplications = new List<Application__c>();
Job__c jobOrder;
List<Id> contactIds = new list<Id>();
list<Attachment> attachments = new list<Attachment>();
if (jobOrder == null) {
existingApplications = [SELECT Id, Candidate_Contact__c FROM Application__c WHERE Job__c =: jobId];
for (Application__c appl : existingApplications) {
contactIds.add(appl.Candidate_Contact__c);
}
attachments = [SELECT Id, Name, Body, ContentType FROM Attachment WHere ParentId in :contactIds ];
}
}
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'secret@secret.com'};
email.setToAddresses(toAddresses);
email.setSubject('Foo');
email.setPlainTextBody('Bar');
List<Messaging.EmailFileAttachment> emailAttachments = new List<Messaging.EmailFileAttachment>();
for(Attachment att : attachments) {
if(att.body != null){
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(att.Name);
efa.setBody(att.Body);
efa.setContentType(att.ContentType);
emailAttachments.add(efa);
}
}
email.setFileAttachments(emailAttachments);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
Interestingly, I still get the same error :
REQUIRED_FIELD_MISSING, No body specified in the file attachment: []
We have defined the email attachment correctly.