You need to sign in to do that
Don't have an account?
Joseph Kennedy
.keyset() method
Hi all,
I just created the following trigger.
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!
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!
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.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
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.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
In your code Set<Id> opportunityIdsWithTask, This is use for hold Id.
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