+ Start a Discussion
VDubVDub 

Method does not exist or incorrect signature Error on Trigger

Hi,

I'm getting an error on my trigger when I call the email class I created.  The Line in red is the one causing the error.  
I'm new to apex and any help would be appreciated .
Thanks, Vanessa

The Error

Method does not exist or incorrect signature: [AccCreatedEmailAlert].sendMail(LIST<Task>, SOBJECT:Task)

Class Code

public class AccCreatedEmailAlert{
public static void sendMail (User u, Task t) {
User u1 =[select Email from User where Id =: t.OwnerId limit 1];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddress = new String[] {u1.Email};
mail.setToAddresses(toAddress);
mail.setSubject('CS-Accommodation Order Task Created');
mail.setPlainTextBody('You are being sent this email because an CS-Accommodation Order Task has been created and assigned to you.\n\nThanks, SFDC');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}


Trigger Code

  User-added image


Best Answer chosen by VDub
MaxPowerForceMaxPowerForce
Trigger.new is a list of tasks, but your SendMail method taks a user and a task as an argument. Try this version of your emailer class and trigger.  I think this will work better because it is bulkified.

trigger CSAccCreatedEmailAlert on Task (after insert) {
        
        
	list<id> userIds = new list<Id>();
	
        //Iterate trigger.new and find the userIds to email blast
        for (task t : trigger.new){
			if (t.Type == 'Maintenance - Quality') {
				userIds.add(t.OwnerId);
			
			}
	}
	
	AccCreatedEmailAlert ac = new AccCreatedEmailAlert();	
	ac.sendMail(userIds);

}



public class AccCreatedEmailAlert{

	public static void sendMail (List<Id userIds){
		
        User[] usersToBlast = [select Email from User where Id in userIds];
		 
		 
		List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
		 
		for (user u : usersToBlast){
			Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
			String[] toAddress = new String[] {u.Email};
			mail.setToAddresses(toAddress);
			mail.setSubject('CS-Accommodation Order Task Created');
			mail.setPlainTextBody('You are being sent this email because an CS-Accommodation Order Task has been created and assigned to you.\n\nThanks, SFDC');
			messages.add(mail);
			}
			
			Messaging.sendEmail(messages);
		}
}

All Answers

kevin Carotherskevin Carothers
Your routine "public static void sendMail()"   is expecting a User and a  Task but you're giving it a list of Tasks and a single task....

Plus, you have a SOQL statement inside a loop - which is not adviseable. 
MaxPowerForceMaxPowerForce
Trigger.new is a list of tasks, but your SendMail method taks a user and a task as an argument. Try this version of your emailer class and trigger.  I think this will work better because it is bulkified.

trigger CSAccCreatedEmailAlert on Task (after insert) {
        
        
	list<id> userIds = new list<Id>();
	
        //Iterate trigger.new and find the userIds to email blast
        for (task t : trigger.new){
			if (t.Type == 'Maintenance - Quality') {
				userIds.add(t.OwnerId);
			
			}
	}
	
	AccCreatedEmailAlert ac = new AccCreatedEmailAlert();	
	ac.sendMail(userIds);

}



public class AccCreatedEmailAlert{

	public static void sendMail (List<Id userIds){
		
        User[] usersToBlast = [select Email from User where Id in userIds];
		 
		 
		List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
		 
		for (user u : usersToBlast){
			Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
			String[] toAddress = new String[] {u.Email};
			mail.setToAddresses(toAddress);
			mail.setSubject('CS-Accommodation Order Task Created');
			mail.setPlainTextBody('You are being sent this email because an CS-Accommodation Order Task has been created and assigned to you.\n\nThanks, SFDC');
			messages.add(mail);
			}
			
			Messaging.sendEmail(messages);
		}
}

This was selected as the best answer
VDubVDub
MaxPowerForce

It's totally working in Production.  Thank you, Thank you, Thank you!!!

Here is the final code.

Class Code to Send Email to Owner of Task

public class AccCreatedEmailAlert {
public void sendMail (List<Id> userIds){
User[] usersToBlast = [select Email from User where Id =: userIds];   
List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
for (user u : usersToBlast){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddress = new String[] {u.Email};
mail.setToAddresses(toAddress);
mail.setSubject('CS-Accommodation Order Task Created');
mail.setPlainTextBody('You are being sent this email because an CS-Accommodation Order Task has been created and assigned to you.\n\nThanks, SFDC');
messages.add(mail);
}
Messaging.sendEmail(messages);
                      }
                      }

Trigger Code

trigger CSAccCreatedEmailAlert on Task (after insert) {
list<id> userIds = new list<Id>();
for (Task t : Trigger.new) {
if (t.Type == 'CS-Accommodation Order') {
userIds.add(t.OwnerId);
  }
}   
AccCreatedEmailAlert ac = new AccCreatedEmailAlert ();      
ac.sendMail(userIds);
}


Test Class Code

@istest
public class TestCreatedTask {
static testMethod void insertNewTask(){
       Task taskToCreate = new Task ();
        taskToCreate.OwnerId = '00560000001GExO';
        taskToCreate.Type = 'CS-Accommodation';
        taskToCreate.Description = 'Please place Accommodation Order';
        taskToCreate.Subject = 'Accommodation Order - Vanessa Pool and Spa';
        taskToCreate.Status = 'Not Started';
        taskToCreate.Priority = 'Normal';
      
         insert taskToCreate;
        
    }

}