You need to sign in to do that
Don't have an account?

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