+ Start a Discussion
Deepak agarwal 9Deepak agarwal 9 

List error

public with sharing class lead_create {
public lead_create(){
set<id> lead_l=new set<id>();
lead_l=[select id from lead where rating like:'%hot%'];
task t=new task(subject='email',whoid=lead_l.id);
insert t;
}
}

Error: Illegal assignment from List<Lead> to Set<Id> at line 4 column 1

Please help!!
Thanks in advance

Ivan BakunIvan Bakun
SOQL query return List<Lead> if you want to get Set<Id> right away you can do something like that 
lead_I = new Map<Id, Lead>([select id from lead where rating like:'%hot%']).keySet();
Hope that answers your question
 
Dilip_VDilip_V
Hi Deepak,

You are assining list values to set.

This query [select id from lead where rating like:'%hot%'] returns list.

Try below code.
public with sharing class lead_create {
public lead_create(){
set<id> lead_l=new set<id>();

For(Lead Lead:[select id from lead where rating like:'%hot%'])
Lead_l.add(Lead.Id);

task t=new task(subject='email',whoid=lead_l.id);
insert t;
}
}

Thanks,
Dilip.

 
Deepak agarwal 9Deepak agarwal 9
Hi dilip,
I am getting these error on your code.
Error: Initial term of field expression must be a concrete SObject: Set<Id> at line 8 column 39
Ivan BakunIvan Bakun
It's because lead__I contains list and you try create only one task. Because you query list of leads you nead to create insert list of tasks. Here is full code.
public with sharing class lead_create {
	public lead_create(){
		set<id> lead_l= (new map<id, lead>([select id from lead where rating like:'%hot%'])).keySet();
		List<task> newTasks = new List<task>();
		for(Id leadId: lead_I) {
			task t=new task(subject='email',whoid=leadId);
			newTasks.add(t);
		}	
		insert newTasks;
		}
	}
}


 
Deepak agarwal 9Deepak agarwal 9
Thanks Ivan
 can u tell me what line 2 is doing exactly..
y cant we use just set y ua using maps again since it contains only ids we can hold them in set?
 
Deepak agarwal 9Deepak agarwal 9
And if i go and check in leads there is no lead being created.
Dilip_VDilip_V
Hi Deepak,

Can you please explain 'what exactly you are trying to do'?

Thanks.
Ivan BakunIvan Bakun
Hi Deepak, 
It gets existing leads via SOQL request, creates map of Ids of this record to records itself and then takes keys (e.g. Ids).
In your code you create Tasks, not leads.
Please clarify your business logic.

Regards,
Ivan