• Trent Michaels
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

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.

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.