0

So I'm not sure if I am just missing something here.. I am trying to just do these as counts so I have the List contactList and then I am trying to then check to see if the id is in ContactUpdateList do have a count of records that are specifically created by our API user. Is there something that I am just not missing?

public static void COIsOnContact(List<Home_Interest__c> COIs){
    
    set<Id> contactIds = new set <Id>();
    
    for(Home_interest__c h :COIs){
        if(h.Contact__c != null){
            contactIds.add (h.Contact__c);          
        }
    }
    
    if(!contactIds.isEmpty()){
        List<Contact> contactList = [Select Id,Primary_COI__c, (Select Id from Community_of_Interest__r ORDER BY CreatedDate ASC) from Contact where Id IN:contactIds FOR UPDATE];
        List<Contact> contactWebList = [Select Id,Primary_COI__c, (Select Id from Community_of_Interest__r where CreatedBy.Username Like '%Web.integrations@meritagehomes%' ORDER BY CreatedDate ASC) from Contact where Id IN:contactIds FOR UPDATE];
        List<Contact> contactUpdateList = new List<Contact>();
        for(Contact c : contactList){
            Boolean isHaveRecs = c.Community_of_Interest__r.size() > 0 ? True : False;
            if(isHaveRecs){
                
                if(c.Primary_COI__c == null){
                    List<Home_Interest__c> HI = c.Community_of_Interest__r;
                    c.Primary_COI__c = HI[0].id;
                }
                
                if(contactWebList.containsKey(ContactList.Id)  ){
                    c.Count_of_Web_COI__c = c.Community_of_Interest__r.size();
                }
                
                contactUpdateList.add(c);
            }
            
        }
        
        if(!contactUpdateList.isEmpty())    update contactUpdateList;
    }
}
2
  • on which line you are getting this error? Jun 18, 2020 at 6:37
  • if(contactWebList.containsKey(ContactList.Id)
    – SoleHack
    Jun 18, 2020 at 6:39

1 Answer 1

2

The contactlist is a list of contacts that is why you are getting the error. Also you just need to use c.Id as you are looping over that list only.

The contactWebList is not a map so you can't call containsKey() on that

You don't need two queries on contacts, just do only one query and collect the count in the map.

The corrected pseudo-code is:

List<Contact> contactList = [Select Id,Primary_COI__c, (Select Id,CreatedBy.Username from Community_of_Interest__r ORDER BY CreatedDate ASC) from Contact where Id IN:contactIds FOR UPDATE];

Map<Id, Integer> webCoiMap = new Map<Id, Integer>();
for(Contact c: contactList){
    Integer count = 0;
    for(Community_of_Interest__c coi: c.Community_of_Interest__r){
        if(coi.CreatedBy.Username.containsIgnoreCase('Web.integrations@meritagehomes')){
            count++;
        }
    }
    if(count != 0){
        webCoiMap.put(c.Id, count);
    }
}

List<Contact> contactUpdateList = new List<Contact>();
for(Contact c : contactList){
    Boolean isHaveRecs = c.Community_of_Interest__r.size() > 0 ? True : False;
    if(isHaveRecs){
        if(c.Primary_COI__c == null){
            List<Home_Interest__c> HI = c.Community_of_Interest__r;
            c.Primary_COI__c = HI[0].id;
        }
        
        if(webCoiMap.containsKey(c.Id)){
            c.Count_of_Web_COI__c = webCoiMap.get(c.Id);
        }
        
        contactUpdateList.add(c);
    }
}
update contactUpdateList;

You must log in to answer this question.

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