You need to sign in to do that
Don't have an account?
Clayton Hilte
Get from Map using a value in a List
Hi all,
I have a Trigger on the Task object. I am trying to perform a get from a Map using an ID from a previoulsy populated List. The list will only every have 1 ID in it, but I still need to tell it which ID to use. Now I could simply use the WhoId, but without going into detail I can't use that for other reasons based on how the trigger will function.
Based on the code below, is this correct?
-------------------------------------------------------------------------
trigger ContactAccountLastActivity on Task (after insert, after update) {
List<String> lstWhoIds = new List<String>();
for( Task t : Trigger.new )
{
// Add the "Who" from the task to the lstWhoIds List
lstWhoIds.add(t.WhoId);
}
// Query Contact Object to get Contact fields and create
// a Map of Contact ID to Contact fields using lstContactFields Query results
List<Contact> lstContactFields = new List<Contact>([SELECT Id, AccountId, Last_Contacted_Date__c, Last_Contacted_By__c
FROM Contact
WHERE Id in :lstWhoIds]);
Map<String, Contact> mapWhoIds = new Map<String, Contact>(lstContactFields);
for( task t : Trigger.new )
{
if( (t.type == 'Telephone In' || t.type == 'Telephone Out' || t.type == 'Visit/Meeting') && t.status == 'Completed' )
{
if( Date.valueOf(t.Completed_Date__c) >= mapWhoIds.get(lstWhoIds).Last_Contacted_Date__c )
{
mapWhoIds.get(lstWhoIds[0]).Last_Contacted_Date__c = Date.valueOf(t.Completed_Date__c);
}
}
}
mapWhoIds.get(lstWhoIds[0]).Last_Contacted_Date__c does not provide any errors
mapWhoIds.get(lstWhoIds).Last_Contacted_Date__c give error: "Incompatible key type LIST<String> for MAP<String, Contact>
I have a Trigger on the Task object. I am trying to perform a get from a Map using an ID from a previoulsy populated List. The list will only every have 1 ID in it, but I still need to tell it which ID to use. Now I could simply use the WhoId, but without going into detail I can't use that for other reasons based on how the trigger will function.
Based on the code below, is this correct?
-------------------------------------------------------------------------
trigger ContactAccountLastActivity on Task (after insert, after update) {
List<String> lstWhoIds = new List<String>();
for( Task t : Trigger.new )
{
// Add the "Who" from the task to the lstWhoIds List
lstWhoIds.add(t.WhoId);
}
// Query Contact Object to get Contact fields and create
// a Map of Contact ID to Contact fields using lstContactFields Query results
List<Contact> lstContactFields = new List<Contact>([SELECT Id, AccountId, Last_Contacted_Date__c, Last_Contacted_By__c
FROM Contact
WHERE Id in :lstWhoIds]);
Map<String, Contact> mapWhoIds = new Map<String, Contact>(lstContactFields);
for( task t : Trigger.new )
{
if( (t.type == 'Telephone In' || t.type == 'Telephone Out' || t.type == 'Visit/Meeting') && t.status == 'Completed' )
{
if( Date.valueOf(t.Completed_Date__c) >= mapWhoIds.get(lstWhoIds).Last_Contacted_Date__c )
{
mapWhoIds.get(lstWhoIds[0]).Last_Contacted_Date__c = Date.valueOf(t.Completed_Date__c);
}
}
}
mapWhoIds.get(lstWhoIds[0]).Last_Contacted_Date__c does not provide any errors
mapWhoIds.get(lstWhoIds).Last_Contacted_Date__c give error: "Incompatible key type LIST<String> for MAP<String, Contact>
Below code will help.used string to find the value as below: Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
All Answers
Below code will help.used string to find the value as below: Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
Thanks,
Anoop
@Balaji - I like the idea of initiatlizing another var String before passing the update values. I don't think there will be any difference, but it is cleaner code and might be better practice. Thanks!