The below code is working as expected but can this code be optimized further? and I'm using three different for loops
in order to make this working.
I'm trying to find out if the Candidate has attachment associated with the record so each candidate record should associate with one or more attachment.
set<Id> setC = new set<Id> ();
//List<Candidate__c> canAttachList = [SELECT id,name (SELECT Id,Name FROM Attachments) FROM Candidate__c WHERE Employee__c ='XXXXX'];
List<Candidate__c> canAttachList = [SELECT id,name FROM Candidate__c WHERE Employee__c ='XXXXX'];
for(Candidate__c c: canAttachList)
{
setC.add(c.Id);
}
map<Id, Attachment> mapAttach = new map<Id, Attachment>([SELECT Id,ParentId,Name FROM Attachment WHERE ParentId IN : setC ]);
for(Id c : mapAttach.keySet())
{
for(Id d : setC)
{
if(mapAttach.get(c).ParentId == d) {
system.debug('attachment exists');
}
else {
system.debug('no attachment presents');
}
}
}