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
Clayton HilteClayton 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>
 
Best Answer chosen by Clayton Hilte
Balaji BondarBalaji Bondar
Hi Clay,
Below code will help.used string to find the value as below:
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 )
                {
                    String lstWhoIdsString = lstWhoIds[0];
					mapWhoIds.get(lstWhoIdsString).Last_Contacted_Date__c = Date.valueOf(t.Completed_Date__c);
                }
        }
}
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

Balaji BondarBalaji Bondar
Hi Clay,
Below code will help.used string to find the value as below:
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 )
                {
                    String lstWhoIdsString = lstWhoIds[0];
					mapWhoIds.get(lstWhoIdsString).Last_Contacted_Date__c = Date.valueOf(t.Completed_Date__c);
                }
        }
}
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.
This was selected as the best answer
Anoop AsokAnoop Asok
The parameter to a Map's get method should be the same type as that of the Key. In your case, the Key is of type string, but the parameter being passed is a List<String>. Changing the RHS to mapWhoIds.get(lstWhoIds[0]).Last_Contacted_Date__c might take out that error message, but to solve your problem I guess you should be using t.whoId instead of listWhoIds[0].

Thanks,
Anoop
Clayton HilteClayton Hilte
@Anoop - I don't get the error when I specify the position in the list such as (lstWhoIds[0]). It seems to me that the error is occurring because it is attempting to put the entire list as a single Unique Key. Testing it with the position [0] worked fine on my debug logs.

@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!