+ Start a Discussion
Trent MichaelsTrent Michaels 

Apex Class: Illegal assignment from List to List

Hello all,

I found some code on how to create an Apex Class that allows users to send an email to salesforce with a specific subject format to create tasks.

The problem is that this code seems to be a little old and I'm having some trouble updating it as I'm not overly familiar with Apex coding. 

 

global class clsInboundEmailHandler implements Messaging.InboundEmailHandler { 

	global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){ 
		Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); 
			
			string subject = email.Subject; 
			string userID = ''; 
			integer userIDNumber = 0; 
			string restOfSubject = ''; 
			if(subject.contains('-') == false){ 
				result.success = false; 
				return result; 
			} 
			list<String> subjectSplit = new list<String>(); 
			subjectSplit = subject.split('-'); 
			userID = subjectSplit[0]; 
			userID = userID.trim(); 
			if(userID.isNumeric() == false){ 
				result.success = false; 
				return result; 
			}else{ 
				userIDNumber = integer.valueOf(userID); 
			} 
			boolean firstOne = true; 
			for(string s : subjectSplit){ 
				if(firstOne){ 
						firstOne = false; 
					}else{ 
						restOfSubject += s; 
					} 
				} 
				
				//Now find the user 
				string userIDForTask = getUserIDForTask(userIDNumber); 
				if(userIDForTask == ''){ 
					result.success = false; 
					return result; 
				} 
				
				Task t = new Task(); 
				t.Subject = restOfSubject; //This is from the subject line 
				t.ActivityDate = date.today(); 
				t.Description = email.plainTextBody; //or email.htmlbody; 
				t.Priority = 'Normal'; 
				t.OwnerId = userIDForTask; //Set to owner 
				t.Status = 'Not Started'; 
				t.IsReminderSet = true; 
				t.ReminderDateTime = System.now() + 1; 
				insert t; 
				
				result.success = true; 
				return result; 
			} 
			
			private string getUserIDForTask(integer userIDNumber){ 
				list<boolean> userList = new list<boolean>(); 
			string userIDForTask = ''; 
			userList = [Select ID 
								From User 
								Where EmailTaskID__c = :userIDNumber]; 
			if(userList.size() <> 1){ 
				return ''; 
				}else{ 
					return userList[0].id; 
				} 
			} 
		}

The problem is at line 58, I am getting the error "Illegal assignment from List to List". I believe the error is related to the way userIDNumber is declared, but for the life of me I cannot figure out the solution.

Would anyone have any suggestions or a solution?

Thank you.
Best Answer chosen by Trent Michaels
Magesh Mani YadavMagesh Mani Yadav
Hi Trent,
Replace line 56 with the line below
list<User> userList = new list<User>(); 

All Answers

Magesh Mani YadavMagesh Mani Yadav
Hi Trent,
Replace line 56 with the line below
list<User> userList = new list<User>(); 
This was selected as the best answer
Nevlyn DSousa TicloNevlyn DSousa Ticlo
Hi Trent,

Ideally the results of a soql query can be stored as follows
1. a single SObject. eg. User u = [Select id from user where {some condition}];
2. a list of sObjects. eg. List<User> userList = [Select id from user where {some condition}];
3. an integer. eg. integer i = [Select COUNT from User where {some Condition}];

In your case
At line 56
list<boolean> userList = new list<boolean>();

can you change that to
list<User> userList = new list<User>();

This should solve your problem.

let me know if that works.
Trent MichaelsTrent Michaels
So the solution worked to resolve errors, but now I'm running into similar problems with the test class.
 
@isTest 
private class clsInboundEmailHandlerTest { 

	static testMethod void clsInboundEmailHandlerTest() { 
		// create a new email and envelope object 
			Messaging.InboundEmail email = new 
			Messaging.InboundEmail() ; 
			Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); 
			
			// setup the data for the email 
			email.subject = 'This is my subject'; 
			email.fromname = 'FirstName LastName'; 
			env.fromAddress = 'someaddress@email.com'; 
			email.plainTextBody = 'Body of the Email'; 
			
			Messaging.InboundEmailResult ierItem; 
			clsInboundEmailHandler emailProcess = new clsInboundEmailHandler(); 
			ierItem = emailProcess.handleInboundEmail(email, env); 
			
			system.assertEquals(false, ierItem.Success); //The subject did not start with a number 
	} 
	
	static testMethod void clsInboundEmailHandlerTest2() { 
		// create a new email and envelope object 
			Messaging.InboundEmail email = new 
			Messaging.InboundEmail() ; 
			Messaging.InboundEnvelope env = new 
			Messaging.InboundEnvelope(); 
			
			// setup the data for the email 
			email.subject = 'A-This is my subject'; 
			email.fromname = 'FirstName LastName'; 
			env.fromAddress = 'someaddress@email.com'; 
			email.plainTextBody = 'Body of the Email'; 
			
			Messaging.InboundEmailResult ierItem; 
			clsInboundEmailHandler emailProcess = new clsInboundEmailHandler(); 
			ierItem = emailProcess.handleInboundEmail(email, env); 
			
			system.assertEquals(false, ierItem.Success); //The subject did not start with a number 
	} 
	
	static testMethod void clsInboundEmailHandlerTest3() { 
		// create a new email and envelope object 
			Messaging.InboundEmail email = new Messaging.InboundEmail() ; 
			Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); 
			
			// setup the data for the email 
			email.subject = '1-This is my subject'; 
			email.fromname = 'FirstName LastName'; 
			env.fromAddress = 'someaddress@email.com'; 
			email.plainTextBody = 'Body of the Email'; 
			
			Messaging.InboundEmailResult ierItem; 
			clsInboundEmailHandler emailProcess = new clsInboundEmailHandler(); 
			ierItem = emailProcess.handleInboundEmail(email, env); 
			
			system.assertEquals(false, ierItem.Success); //The id in the subject line was not found 
	} 
	
	static testMethod void clsInboundEmailHandlerTest4() { 
		// create a new email and envelope object 
			Messaging.InboundEmail email = new Messaging.InboundEmail() ; 
			Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); 
			
			// setup the data for the email 
			email.subject = '1-This is my subject'; 
			email.fromname = 'FirstName LastName'; 
			env.fromAddress = 'someaddress@email.com'; 
			email.plainTextBody = 'Body of the Email'; 
			User u = CreateUser(1); 
			
			Messaging.InboundEmailResult ierItem; 
			clsInboundEmailHandler emailProcess = new clsInboundEmailHandler(); 
			ierItem = emailProcess.handleInboundEmail(email, env); 
			
			system.assertEquals(true, ierItem.Success); //The id in the subject line was not found 
			list<String> taskList = new list<String>(); 
			taskList = [Select ID, Subject, Description 
									From Task 
									Where OwnerID = :u.ID]; 
			system.assertEquals(1, taskList.size()); 
			system.assertEquals(taskList[0].Subject, 'This is my subject'); 
			system.assertEquals(taskList[0].Description, 'Body of the Email'); 
	} 
	
	private static User CreateUser(integer numberForEmail){	
				Profile p = [SELECT Id FROM profile WHERE name='System Administrator']; 
		User u = new User(alias = 'newSUser', email='newSAuser@testorg.com', 
			emailencodingkey='UTF-8', lastname='SATesting', languagelocalekey='en_US', 
			localesidkey='en_US', profileid = p.Id, 
			timezonesidkey='America/Los_Angeles', username='newSAuser@testorg.com', 
			CommunityNickname='CN'); 
		u.EmailTaskID__c = numberForEmail; 
		u.FirstName = 'TestUser'; 
				u.LastName = 'TestLastuser'; 
				insert u; 
				return u; 
		} 
	}

Line 79 is throwing the same error "Illegal Assignment from List to List"
Nevlyn DSousa TicloNevlyn DSousa Ticlo
Line 78
list<String> taskList = new list<String>();
change to
list<Task> taskList = new list<Task>();
Magesh Mani YadavMagesh Mani Yadav
Hi Trent,

You need to replace liine 78 with
list<Task> taskList = new list<Task>();
 
Trent MichaelsTrent Michaels
I can't beleive how wonderful you guys are. Thank you, that solved an issue that's been plaguing me for a few days!