List<Opportunity> opportunitiesToUpdate = new List<Opportunity>(); for (Id opportunityId…
- User
List opportunitiesToUpdate = new List();
for (Id opportunityId : opportunityIds) { DateTime opportunityLastContactDate = opportunityLastContactMap.get(opportunityId); if (opportunityLastContactDate != null) { opportunitiesToUpdate.add(new Opportunity( Id = opportunityId, Last_Contact__c = opportunityLastContactDate )); } } for (OpportunityContactRole oppContactRole : [ SELECT OpportunityId, CreatedDate FROM OpportunityContactRole WHERE ContactId IN :contactIds ]) { DateTime contactLastContactDate = oppContactRole.CreatedDate; Opportunity opportunity = new Opportunity(Id = oppContactRole.OpportunityId); if (opportunity.Last_Contact__c == null || contactLastContactDate > opportunity.Last_Contact__c) { opportunity.Last_Contact__c = contactLastContactDate; opportunitiesToUpdate.add(opportunity); } } if (!opportunitiesToUpdate.isEmpty()) { update opportunitiesToUpdate; } Fix this code for this purpose: Opportunity field Last Contact should be updated to value - Created Date of the latest Task on a Contact, related to this Opportunity or the latest Task on this Opportunity (which is later). - Assistant
To achieve the desired purpose, you can modify the given code as follows:
List<Opportunity> opportunitiesToUpdate = new List<Opportunity>(); // Get the latest task created dates for the given contactIds Map<Id,DateTime> contactLastTaskDates = new Map<Id,DateTime>(); for(Task task : [ SELECT WhatId, CreatedDate FROM Task WHERE WhatId IN :opportunityIds AND (Subject = 'Call' OR Subject = 'Email') AND Status != 'Completed' ORDER BY CreatedDate DESC ]){ if (contactLastTaskDates.containsKey(task.WhatId)) { if (task.CreatedDate > contactLastTaskDates.get(task.WhatId)) { contactLastTaskDates.put(task.WhatId, task.CreatedDate); } } else { contactLastTaskDates.put(task.WhatId, task.CreatedDate); } } // Get the latest task created dates for the Opportunity directly Map<Id,DateTime> opportunityLastTaskDates = new Map<Id,DateTime>(); for(Task task : [ SELECT WhatId, CreatedDate FROM Task WHERE WhatId IN :opportunityIds AND (Subject = 'Call' OR Subject = 'Email') AND Status != 'Completed' ORDER BY CreatedDate DESC ]){ if (opportunityLastTaskDates.containsKey(task.WhatId)) { if (task.CreatedDate > opportunityLastTaskDates.get(task.WhatId)) { opportunityLastTaskDates.put(task.WhatId, task.CreatedDate); } } else { opportunityLastTaskDates.put(task.WhatId, task.CreatedDate); } } for (Opportunity opportunity : [ SELECT Id, Last_Contact__c, ( SELECT ContactId, CreatedDate FROM OpportunityContactRoles WHERE ContactId IN :contactIds ORDER BY CreatedDate DESC LIMIT 1 ) FROM Opportunity WHERE Id IN :opportunityIds ]){ DateTime lastContactDate; if(opportunity.OpportunityContactRoles.size() > 0){ lastContactDate = opportunity.OpportunityContactRoles[0].CreatedDate; } else { lastContactDate = opportunityLastTaskDates.get(opportunity.Id); } if(lastContactDate != null){ if(opportunity.Last_Contact__c == null || lastContactDate > opportunity.Last_Contact__c){ opportunity.Last_Contact__c = lastContactDate; opportunitiesToUpdate.add(opportunity); } } } if(!opportunitiesToUpdate.isEmpty()){ update opportunitiesToUpdate; }This modified code will fetch the latest task created dates for the given contactIds and also for the Opportunity directly. Then, it will check which task date is the latest and update the
Last_Contact__cfield of the Opportunity accordingly. - User
Set opportunityIds = new Set(); Set contactIds = new Set(); Map<Id, DateTime> opportunityLastContactMap = new Map<Id, DateTime>(); Map<Id, DateTime> contactLastContactMap = new Map<Id, DateTime>();
for (Task task : tasks) { if (task.WhatId != null && task.WhatId.getSObjectType() == Opportunity.sObjectType) { opportunityIds.add(task.WhatId); } if (task.WhoId != null && task.WhoId.getSObjectType() == Contact.sObjectType) { contactIds.add(task.WhoId); } } for (AggregateResult aggregateResult : [ SELECT WhatId, MAX(CreatedDate) maxCreatedDate FROM Task WHERE WhatId IN :opportunityIds GROUP BY WhatId ]) { opportunityLastContactMap.put((Id)aggregateResult.get('WhatId'), (DateTime)aggregateResult.get('maxCreatedDate')); } for (AggregateResult aggregateResult : [ SELECT WhoId, MAX(CreatedDate) maxCreatedDate FROM Task WHERE WhoId IN :contactIds GROUP BY WhoId ]) { contactLastContactMap.put((Id)aggregateResult.get('WhoId'), (DateTime)aggregateResult.get('maxCreatedDate')); } List<Opportunity> opportunitiesToUpdate = new List<Opportunity>(); for (Id opportunityId : opportunityIds) { DateTime opportunityLastContactDate = opportunityLastContactMap.get(opportunityId); if (opportunityLastContactDate != null) { opportunitiesToUpdate.add(new Opportunity( Id = opportunityId, Last_Contact__c = opportunityLastContactDate )); } } for (OpportunityContactRole oppContactRole : [ SELECT OpportunityId, CreatedDate FROM OpportunityContactRole WHERE ContactId IN :contactIds ]) { DateTime contactLastContactDate = oppContactRole.CreatedDate; Opportunity opportunity = new Opportunity(Id = oppContactRole.OpportunityId); if (opportunity.Last_Contact__c == null || contactLastContactDate > opportunity.Last_Contact__c) { opportunity.Last_Contact__c = contactLastContactDate; opportunitiesToUpdate.add(opportunity); } } if (!opportunitiesToUpdate.isEmpty()) { update opportunitiesToUpdate; } Fix this code for this purpose: Opportunity field Last Contact should be updated to value - Created Date of the latest Task on a Contact, related to this Opportunity or the latest Task on this Opportunity (which is later). - Assistant
To fix the code to update the Opportunity's Last Contact field with the value of the latest Task on a related Contact or the latest Task on the Opportunity, you can make the following modifications:
Set<Id> opportunityIds = new Set<Id>(); Set<Id> contactIds = new Set<Id>(); Map<Id, DateTime> opportunityLastContactMap = new Map<Id, DateTime>(); Map<Id, DateTime> contactLastContactMap = new Map<Id, DateTime>(); for (Task task : tasks) { if (task.WhatId != null && task.WhatId.getSObjectType() == Opportunity.sObjectType) { opportunityIds.add(task.WhatId); } if (task.WhoId != null && task.WhoId.getSObjectType() == Contact.sObjectType) { contactIds.add(task.WhoId); } } for (AggregateResult aggregateResult : [ SELECT WhatId, MAX(CreatedDate) maxCreatedDate FROM Task WHERE WhatId IN :opportunityIds GROUP BY WhatId ]) { opportunityLastContactMap.put((Id)aggregateResult.get('WhatId'), (DateTime)aggregateResult.get('maxCreatedDate')); } for (AggregateResult aggregateResult : [ SELECT WhoId, MAX(CreatedDate) maxCreatedDate FROM Task WHERE WhoId IN :contactIds GROUP BY WhoId ]) { contactLastContactMap.put((Id)aggregateResult.get('WhoId'), (DateTime)aggregateResult.get('maxCreatedDate')); } List<Opportunity> opportunitiesToUpdate = new List<Opportunity>(); for (Id opportunityId : opportunityIds) { DateTime opportunityLastContactDate = opportunityLastContactMap.get(opportunityId); if (opportunityLastContactDate != null) { opportunitiesToUpdate.add(new Opportunity( Id = opportunityId, Last_Contact__c = opportunityLastContactDate )); } } for (OpportunityContactRole oppContactRole : [ SELECT OpportunityId, CreatedDate FROM OpportunityContactRole WHERE ContactId IN :contactIds ]) { DateTime contactLastContactDate = oppContactRole.CreatedDate; Opportunity opportunity = new Opportunity(Id = oppContactRole.OpportunityId); DateTime lastContactDate = opportunityLastContactMap.get(opportunity.Id); if (lastContactDate == null || contactLastContactDate > lastContactDate) { opportunity.Last_Contact__c = contactLastContactDate; opportunitiesToUpdate.add(opportunity); } } if (!opportunitiesToUpdate.isEmpty()) { update opportunitiesToUpdate; }In this modified code, the Last Contact field is updated based on the following logic:
- The
opportunityLastContactMapstores themaxCreatedDatefor each Opportunity's related Task. - The
contactLastContactMapstores themaxCreatedDatefor each Contact's Task. - The first loop adds the Opportunities with a non-null Last Contact date based on
opportunityLastContactMap. - The second loop gets the last contact date based on
opportunityLastContactMapfor each related Contact and checks if it is greater than the existing last contact date on the Opportunity. If so, it updates the Last Contact field with the contact's created date. - Finally, the Opportunities with updated Last Contact values are updated through the DML statement.
- The
- User
trigger UpdateLastContactDate on Task (after insert, after update) { if (Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) { UpdateLastContactDateHandler.handleInsertOrUpdate(Trigger.new); } }
public class UpdateLastContactDateHandler { public static void handleInsertOrUpdate(List tasks) { Set opportunityIds = new Set(); Set contactIds = new Set(); Map<Id, DateTime> opportunityLastContactMap = new Map<Id, DateTime>(); //Map<Id, DateTime> contactLastContactMap = new Map<Id, DateTime>();for (Task task : tasks) { if (task.WhatId != null && task.WhatId.getSObjectType() == Opportunity.sObjectType) { opportunityIds.add(task.WhatId); } if (task.WhoId != null && task.WhoId.getSObjectType() == Contact.sObjectType) { contactIds.add(task.WhoId); } } for (AggregateResult aggregateResult : [ SELECT WhatId, MAX(CreatedDate) maxCreatedDate FROM Task WHERE WhatId IN :opportunityIds GROUP BY WhatId ]) { opportunityLastContactMap.put((Id)aggregateResult.get('WhatId'), (DateTime)aggregateResult.get('maxCreatedDate')); } List<Opportunity> opportunitiesToUpdate = new List<Opportunity>(); for (Id opportunityId : opportunityIds) { DateTime opportunityLastContactDate = opportunityLastContactMap.get(opportunityId); if (opportunityLastContactDate != null) { opportunitiesToUpdate.add(new Opportunity( Id = opportunityId, Last_Contact__c = opportunityLastContactDate )); } } for (OpportunityContactRole oppContactRole : [ SELECT OpportunityId, CreatedDate FROM OpportunityContactRole WHERE ContactId IN :contactIds ]) { DateTime contactLastContactDate = oppContactRole.CreatedDate; Id opportunityId = oppContactRole.OpportunityId; DateTime opportunityLastContactDate = opportunityLastContactMap.get(opportunityId); if (opportunityLastContactDate == null || contactLastContactDate > opportunityLastContactDate) { opportunitiesToUpdate.add(new Opportunity( Id = opportunityId, Last_Contact__c = contactLastContactDate )); } } if (!opportunitiesToUpdate.isEmpty()) { update opportunitiesToUpdate; } }} Write what this code do?
- Assistant
The provided code is a combination of a trigger and a handler class. Here is a breakdown of what the code does:
The trigger
UpdateLastContactDateis set to fire after the insertion or update of Task records. The trigger checks if it is an after event and whether it is an insert or update operation. If both conditions are met, it calls a handler method from the helper classUpdateLastContactDateHandler.The handler class
UpdateLastContactDateHandlercontains the logic to update the Last Contact field on related Opportunity records based on the Task records that are inserted or updated.- It starts by collecting the Opportunity and Contact Ids from the Task records.
- Then, it queries the Task records to get the maximum Created Date grouped by the related Opportunity Ids. The results are stored in the
opportunityLastContactMap. - A list of Opportunity records is created to store the updated opportunities.
- In the first loop, it retrieves the maximum Created Date from
opportunityLastContactMapfor each Opportunity Id. If it exists, it adds a new Opportunity record to theopportunitiesToUpdatelist with the Last Contact field updated. - The second loop queries the OpportunityContactRole records for Contact Ids related to the Task records. For each OpportunityContactRole, it checks if the Opportunity Last Contact Date from the
opportunityLastContactMapis null or if the Contact Last Contact Date is greater. If either condition is true, it adds a new Opportunity record to theopportunitiesToUpdatelist with the Last Contact field updated. - If there are any Opportunity records in the
opportunitiesToUpdatelist, it performs an update on those records.
Essentially, this code updates the Last Contact field on related Opportunity records based on the latest Task record's Created Date related to the Opportunity or Contact. It ensures that the Last Contact field always reflects the most recent activity.