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
AviKesariAviKesari 

.keyset() returns null value when I query and store in List

Hi all,
I'm trying to get the account ID's from contact(lookup realtionship not Master detail).

 public void updateAccountActivityFields(Task[] taskRecords){
 Set<String> conIDs = new Set<String>();
 for(Task t : taskRecords){
            conIDs.add(t.WhoID);
        } 
//query outside forloop
//Account and contact have lookup relationship
 Map<Id,Contact> conmap = new Map<Id,Contact>([SELECT AccountId FROM Contact WHERE Id IN :conIDs]);

//returns NO value for accountsToUpdate, But When I debug only for conmap.keyset() I see the account ID..need help!!
List<Account> accountsToUpdate = [SELECT Id FROM Account WHERE Id IN :conmap.keyset()];
            
 for(Task t: [SELECT Id, LastModifiedBy.name, type, LastModifiedDate, CreatedDate, RecordTypeId, WhoID, WhatID FROM task WHERE id in :taskRecords]){
for(Account a : accountsToUpdate){
//Logic here
}
}
Best Answer chosen by AviKesari
Maharajan CMaharajan C
Hi AVi,

conMap.Keyset() will Store the contact Id not the Account Ids in Key. So that is Account list doesnot return any values.

please try the below changes it will work:

public void updateAccountActivityFields(Task[] taskRecords){
 Set<String> conIDs = new Set<String>();
 for(Task t : taskRecords){
            conIDs.add(t.WhoID);
        } 
//query outside forloop
//Account and contact have lookup relationship
 Map<Id,Contact> conmap = new Map<Id,Contact>([SELECT AccountId FROM Contact WHERE Id IN :conIDs]);

Set<Id> AccIdSet = new set<Id>();

for(Contact c: conmap.values())
{
AccIdSet.add(c.AccountId);
}


List<Account> accountsToUpdate = [SELECT Id FROM Account WHERE Id IN :AccIdSet];
            
for(Task t: [SELECT Id, LastModifiedBy.name, type, LastModifiedDate, CreatedDate, RecordTypeId, WhoID, WhatID FROM task WHERE id in :taskRecords]){
for(Account a : accountsToUpdate){
//Logic here
}
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi AVi,

conMap.Keyset() will Store the contact Id not the Account Ids in Key. So that is Account list doesnot return any values.

please try the below changes it will work:

public void updateAccountActivityFields(Task[] taskRecords){
 Set<String> conIDs = new Set<String>();
 for(Task t : taskRecords){
            conIDs.add(t.WhoID);
        } 
//query outside forloop
//Account and contact have lookup relationship
 Map<Id,Contact> conmap = new Map<Id,Contact>([SELECT AccountId FROM Contact WHERE Id IN :conIDs]);

Set<Id> AccIdSet = new set<Id>();

for(Contact c: conmap.values())
{
AccIdSet.add(c.AccountId);
}


List<Account> accountsToUpdate = [SELECT Id FROM Account WHERE Id IN :AccIdSet];
            
for(Task t: [SELECT Id, LastModifiedBy.name, type, LastModifiedDate, CreatedDate, RecordTypeId, WhoID, WhatID FROM task WHERE id in :taskRecords]){
for(Account a : accountsToUpdate){
//Logic here
}
}

Thanks,
Maharajan.C
This was selected as the best answer
AviKesariAviKesari
Thanks Maharajan :)