function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Reuben Reynoso 3Reuben Reynoso 3 

Illegal assignment from List<CampaignMember> to List<CampaignMember>

I was running into this error in some code of mine and so I began to do some troubleshooting.  Hoping someone can explain what is going on.

So while troubleshooting, I opened an execute anonymous window to test a theory; this error only appears when referencing CampaignMember and not other objects.  Test Code 1 below results in the error "Illegal assignment from List<CampaignMember> to List<CampaignMember>"  while test code 2 executes with no errors. 

What is the difference and how can get around this issues?

------------Test Code 1:-----------------
string ID='xyz';
list<CampaignMember> lst = new List<CampaignMember>();
lst = [Select id from CampaignMember where id =:ID];


--------------Test Code 2:-----------------
string ID='xyz';
list<Campaign> lst = new List<Campaign>();
lst = [Select id from Campaign where id =:ID];
Best Answer chosen by Reuben Reynoso 3
Ankit SehgalAnkit Sehgal
Check if there is already an apex class with name "CampaignMember" in your org. If there is just rename it and your code should work fine

All Answers

Ankit SehgalAnkit Sehgal
Check if there is already an apex class with name "CampaignMember" in your org. If there is just rename it and your code should work fine
This was selected as the best answer
Reuben Reynoso 3Reuben Reynoso 3
Yep, sure did find a class with the exact same name.   So the moral of the story here is to never create a class with the exact same name as an object.