Is there a special way that you need to query the notes object in association to lead or contact? I am trying to query a certain created by & subject on the notes field and then take the count of those API web submissions and update the lead. In my query though, it is not letting me have that relationship to the notes object. What am I missing?
Error: (Select Body,Id,ParentId,Title from Note__r ^ ERROR at Row:2:Column:72 Didn't understand relationship 'Note__r' 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.
public class Notefunctions {
public static void countofNotesInLead(List<Note> Nte){
Set<Id> parentIds = new Set<Id>();
for(Note n :Nte) {
if(n.ParentId != null){
parentIds.add(n.ParentId);
}
}
if(!parentIds.isEmpty()){
List<Lead> leadList = [Select Id, Count_of_Tour_Requests__c, Count_of_Web_Submissions__c,
(Select Body,Id,ParentId,Title from Note__r
WHERE Title LIKE '%Web Submission%'
AND CreatedBy.Username LIKE '%web.integrations@%')
from Lead where Id IN:parentIds FOR UPDATE];
Map<Id, Integer> tourNoteMap = new Map<Id, Integer>();
for(Lead lead: leadList){
Integer count = 0;
for(Note note: lead.Note__r){
if(note.Body.containsIgnoreCase('In Person Tour') || note.Body.containsIgnoreCase('Virtual Tour')){
count++;
}
}
if(count != 0){
tourNoteMap.put(lead.Id, count);
}
}
List<Lead> leadUpdateList = new List<Lead>();
for(Lead l : leadList){
Boolean isHaveRecs = l.Note__r.size() > 0 ? True : False;
if(isHaveRecs){
l.Count_of_Web_Submissions__c = l.Note__r.size();
if(tourNoteMap.containsKey(l.Id)){
l.Count_of_Tour_Requests__c = tourNoteMap.get(l.Id);
}
leadUpdateList.add(l);
}
}
if(!leadUpdateList.isEmpty()) update leadUpdateList;
}
}
}