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
Joseph KennedyJoseph Kennedy 

.keyset() method

Hi all, 

I just created the following trigger.
 
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;
                         
   }
     }

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!

 
Best Answer chosen by Joseph Kennedy
Khan AnasKhan Anas (Salesforce Developers) 
Hi Joseph,

Greetings to you!

keyset() returns a set that contains all of the keys in the map.

Example:
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();

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.htm

Set 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.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Joseph,

Greetings to you!

keyset() returns a set that contains all of the keys in the map.

Example:
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();

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.htm

Set 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.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Joseph,

In your code Set<Id> opportunityIdsWithTask, This is use for hold Id.
new Map<Id, AggregateResult>([
    SELECT WhatId Id FROM Task
    WHERE WhatId IN :trigger.new
    GROUP BY WhatId
]),

This will return the Map of AggregateResult Id and AggregateResult itself.
And as in the code you are using keySet(), this will retuen all the keys i.e "id" present in the map.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi