1

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;
            
        }
    }
}

1 Answer 1

1

Use Notes instead of the Note__r. The __r is used only in case the relation is customized and not standard. For standard salesforce always use the plural label, like Note, Contacts, Opportunities, and so on.

So your query will be

Select Id, Count_of_Tour_Requests__c, Count_of_Web_Submissions__c, (Select Body,Id,ParentId,Title from Notes WHERE Title LIKE '%Web Submission%' AND CreatedBy.Username LIKE '%web.integrations@%') from Lead where Id IN:parentIds FOR UPDATE

Also, you can query this using contentVersion, contentDocument, which is replacement of the old Notes and Attachments objects. You can find more details about content version, content document in this post.

Content Version Docs

2
  • I feel like an idiot... thanks! Apparently, if I use the plural version it fixed it. I appreciate your help! Commented Jun 19, 2020 at 7:43
  • @JoshAnderson never mind it happens! Commented Jun 19, 2020 at 7:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .