function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Joshua Anderson 39Joshua Anderson 39 

SOQL issue with Note object

Trying to have a method that counts and then updates fields on the Lead object with the notes have been created by ouur API service. We have two fields on the lead for Tour Count and Web Submission count but no matter what I try I keep getting this error. Am I calling the SOQL wrong here? 

Error: 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.

as well as Variable does not exist: Note__r
 
public class Notefunctions {     
    
    public static void countofNotesInLead(List<Note> Nte){
        
        Set<Id> parentIds = new Set<Id>();

		for(Note n :Nte) {
			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;
            
        }
    }
}

 
AnudeepAnudeep (Salesforce Developers) 
Double-check your child relationship name. A similar issue is answered here

Anudeep
Dushyant SonwarDushyant Sonwar
Hi Joshua,

Notes is standard object so you do not need __r for parent-child relationship.

You need to replace Note__r to Notes

Hope this helps.
Dushyant SonwarDushyant Sonwar
Joshua,

Did you try with above solution, Does it solve your purpose?