• Joshua Anderson 39
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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;
            
        }
    }
}