It proved tricky to arrive here as I had to step through all of the errors I was recieving in developer console to arrive at this point where my trigger would successfully compile. I was attempting to reference the Task in the nested SOQL query but quickly learned that Tasks can't be used in joined inner selects. That led to me searching and finding the set portion of this trigger. Would someone mind helping me understand what exactly is going on with this portion of the trigger? I understand everything except for the .keyset() method at the end. Thanks in advance!trigger ClosedOpportunityTrigger on Opportunity(after insert, after update) {
List<Task> taskList = new List<Task>();
// Create a set of Opportunities with no Tasks
Set<Id> opportunityIdsWithTask = new Map<Id, AggregateResult>([
SELECT WhatId Id FROM Task
WHERE WhatId IN :trigger.new
GROUP BY WhatId
]).keySet();
// Add an Task for each Opportunity if it doesn't already have one.
// Iterate over Opportunities that are in this trigger but that don't have Tasks.
for (Opportunity a : [SELECT Id,Name FROM Opportunity
WHERE Id IN :Trigger.New AND
Id NOT IN :opportunityIdsWithTask]) {
taskList.add(new Task(Subject = 'Follow Up Test Task',
whatId = a.Id));
}
if (taskList.size() > 0) {
insert taskList;
}
}
Hi Joseph,Greetings to you!keyset() returns a set that contains all of the keys in the map.Example:
The above example will return the list of the keys i.e red and blue.https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_map.htmSet represents a collection of unique elements with no duplicate values. In your scenario, keyset will return the whatId from Task object.I hope it helps you.Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.Map<String, String> colorCodes = new Map<String, String>();
colorCodes.put(‘Red’, ‘FF0000’);
colorCodes.put(‘Blue’, ‘0000A0’);
Set <String> colorSet = new Set<String>();
colorSet = colorCodes.keySet();
Thanks and Regards,
Khan Anas