+ Start a Discussion
JoshTonksJoshTonks 

Im having trouble with Lists Line 7 Illegal assignment from List to List

Im still learning and i must be doing something wrong im getting an Illegal assignment from List to List on the following code on line 7. Also on a second note does anyone have a link for any books on apex or courses that cover the list functionality please.
 
public with sharing class monthlylist {
	public list<Opportunity> Monthly;
    Date startOfMonth = Date.today().toStartOfMonth();
	Date startOfNextMonth = startOfMonth.addMonths(1);
    
    public list<Opportunity> Monthly(){
    Monthly = [SELECT Owner.Name, COUNT(id)
               FROM Opportunity
              WHERE StageName = :'Closed Won, Complete' AND CloseDate >= :startOfMonth AND CloseDate < :startOfNextMonth AND UK_Canada__c = 'UK'
              GROUP BY Owner.Name];
    return Monthly;
}
}

 
Best Answer chosen by JoshTonks
Waqar Hussain SFWaqar Hussain SF
public with sharing class monthlylist {
	public list<Opportunity> Monthly {get; set;}
    Date startOfMonth = Date.today().toStartOfMonth();
	Date startOfNextMonth = startOfMonth.addMonths(1);
    
    public list<Opportunity> GetMonthly(){
    list<Opportunity> opps = new list<Opportunity>([SELECT Owner.Name, COUNT(id)
               FROM Opportunity
              WHERE StageName = :'Closed Won, Complete' AND CloseDate >= :startOfMonth AND CloseDate < :startOfNextMonth AND UK_Canada__c = 'UK'
              GROUP BY Owner.Name]);
    return opps;
}
}

 

All Answers

Waqar Hussain SFWaqar Hussain SF
try below code
public with sharing class monthlylist {
	public list<Opportunity> Monthly {get; set;};
    Date startOfMonth = Date.today().toStartOfMonth();
	Date startOfNextMonth = startOfMonth.addMonths(1);
    
    public list<Opportunity> GetMonthly(){
    list<Opportunity> opps = new list<Opportunity>([SELECT Owner.Name, COUNT(id)
               FROM Opportunity
              WHERE StageName = :'Closed Won, Complete' AND CloseDate >= :startOfMonth AND CloseDate < :startOfNextMonth AND UK_Canada__c = 'UK'
              GROUP BY Owner.Name]);
    return opps;
}
}

 
JoshTonksJoshTonks
Hi Waqar Hussain that has got rid of that problem so thank you very much however it has created a Expecting '}' but was: ';'.
Waqar Hussain SFWaqar Hussain SF
public with sharing class monthlylist {
	public list<Opportunity> Monthly {get; set;}
    Date startOfMonth = Date.today().toStartOfMonth();
	Date startOfNextMonth = startOfMonth.addMonths(1);
    
    public list<Opportunity> GetMonthly(){
    list<Opportunity> opps = new list<Opportunity>([SELECT Owner.Name, COUNT(id)
               FROM Opportunity
              WHERE StageName = :'Closed Won, Complete' AND CloseDate >= :startOfMonth AND CloseDate < :startOfNextMonth AND UK_Canada__c = 'UK'
              GROUP BY Owner.Name]);
    return opps;
}
}

 
This was selected as the best answer