+ Start a Discussion

Add ids from a list to a set without a for loop

I am looking for a way to to add Look up Ids from a query to a set without using a for loop.  I am essentially looking for the most efficient way to write the code below.  I have found techniques using keyset() but that only pulls in the Id of the task relation object.

set<Id> retActivityIdList = new Set<Id>();
List<taskRelation> taskRelationList = [SELECT TaskId FROM taskRelation WHERE relationId = :contId ORDER BY CreatedDate DESC ];
          for(taskRelation tr: taskRelationList){
Best Answer chosen by andyaldis1.3969086903835708E12
Wilfredo Morillo 20Wilfredo Morillo 20
You can try this:
//Map all the tasks:
map<id,task> reActivityIdMap = new map<id,task>([select id FROM task WHERE id in (SELECT TaskId  FROM taskRelation where id in:contId)]);
set<id>reActivityIdSet = new set<id>();

//Pass all keys to the set. 


Let me know if that helps you.