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 

Trigger doesn't like batch task update from Trigger

I have a snippet of my main code below. When creating/updating a task, trigger updates fields on the related contact and the related contacts parent account (this is different than using the standard WhoId and AccountId and WhatId).

Trigger works fantastic except when users do mass updates, it will only update the 1st record in the batch. I think this is because I have to iterate over my list.

I have a <LIST> that contains Account Ids and a <MAP> that contains the Account Id and the related fields needed for each ID. How can I edit the trigger below to check if an ID from the List matches an ID in the MAP? Without specifying a specific Location in the LIST with [0], it will not pass syntax: "Incompatible key type LIST<String> for MAP<Id,Account>"

How do I change this, or iterate over the Map incase there are multiple tasks being updated in the same batch?

List<String> lstStoreIds = new List<String>();
List<Account> lstStoreFields = new List<Account>([SELECT Id, OwnerId, ParentId, Last_Contacted_By__c, Last_Contact_Owner__c, Last_Contact_All__c FROM Account WHERE Id in :lstStoreIds]);
    
Map<Id, Account> mapStoreIds = new Map<Id, Account>();
        mapStoreIds.putAll(lstStoreFields);

if( mapStoreIds != null )
                {                    
                    
                    Account store = mapStoreIds.get(lstStoreIds[0]);        //error occurs here            
                    if( store.OwnerId == t.OwnerId )
                    {
                        store.Last_Contacted_By__c = t.OwnerId;
                    }
 
James LoghryJames Loghry
The error is because you need to speify the Id of the record, not the record itself. For instance:
 
Account store = mapStoreIds.get(lstStoreIds[0].Id);

Furthermore, instead of using the putAll method, why not instanciate the map with the list of accounts to begin with?  This will save you a couple variables and clean up your code a bit.
 
Map<Id,Account> mapStoreIds = new Map<Id,Account>(
    [Select
        Id
        ,OwnerId
        ,ParentId
        ,Last_Contacted_By__c
        ,Last_Contact_Owner__c
        ,Last_Contact_All__c 
     From 
        Account 
     Where 
        Id in :lstStoreIds]
);


 
Niket SFNiket SF
Hello Clay,

 It seems your code is not bulk safe that means we are using only first record from the lstStorelds[0] (It will always gives you first record)

You have to fine tune the below code. I am just putting some sutaible code.

List<String> lstStoreIds = new List<String>();

Map<Id, Account> mapStoreIds = new Map<Id, Account>([SELECT Id, OwnerId, ParentId, Last_Contacted_By__c, Last_Contact_Owner__c, Last_Contact_All__c FROM Account WHERE Id in :lstStoreIds]);
        mapStoreIds.putAll(lstStoreFields);

if( mapStoreIds != null )
 {

   For(Id AccId : mapStoreIds.KeySet())
{
     Account store = mapStoreIds.get(AccId);
    if( store.OwnerId == t.OwnerId )
    {
          store.Last_Contacted_By__c = t.OwnerId;

       // Your Other Logic
    }
}                   
                   

Regards,
Niket