I am getting below error for the line. Line:

 Set<Id> Calling= 
            [SELECT WhatId FROM Task WHERE Status != 'Completed' and subject='Call'];


Error Error: Compile Error: Illegal assignment from List to Set at line

Can someone suggest how to form it here?

1 Answer 1


The SOQL query returns a list of sObject records by default, so you'll need to add the results in a list of sObject records, then iterate through and add the Ids to a set:

Set <Id> calling = new Set <Id> ();
for (Task task : [SELECT WhatId FROM Task WHERE Status != 'Completed' and subject='Call'])

If you want the actual sObject records into a set, you can use the list.addAll() method:

Set <Task> tasks = new Set <Task> ();
tasks.addAll([SELECT WhoId FROM Task]);

In addition to this, if you need the sObject record IDs (in this case the actual task Id) in a set, you can put the SOQL query result straight into a map:

Map <Id, Task> tasks = new Map <Id, Task> ([SELECT Id FROM Task]);
// tasks.keySet() is a set of Task Ids 

Note the difference between a Set and a List - A set contains unordered elements and no duplicates, whereas a list contains ordered and indexed elements allowing duplicates.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .