You need to sign in to do that
Don't have an account?

Send email failed: REQUIRED_FIELD_MISSING, Add a recipient to send an email.: [] Class.AccTaskTrigr.AccountTaskTrigger: line 38 column 1
Hi to all,
I am a newbee here having problem to ececute a scenario from my institution.
When a new Account record is inserted check wheather any duplicate Account exists based on Account Name,Industry if duplicate record found throw error message. if duplicate records are notfound, check if type=Prospect, if so Create new Task for the owner of the account record and send email alert regarding new task. I have no idea ,I have put the toaddress but it is asking for receipent.
Above is the error when i tried to insert account. and my code with class and trigger are
Trigger:trigger AccTaskTrigr on Account (After insert ) { // Trigger code is good
AccTaskTrigr.AccountTaskTrigger(Trigger.New);}
class:
public class AccTaskTrigr {
public static void AccountTaskTrigger(List<Account> newtrigger){
List<Task> ListTasks = new List<Task>();
Task tsk =new task();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
list<account> accs =[select Name, Industry from Account];
For(account account1: newtrigger){
for(account a :accs){
if(a.name!=account1.name && a.Industry !=account1.Industry){
if( account1.Type=='Prospect'){
tsk.OwnerId=account1.OwnerId;
tsk.Priority='High';
tsk.Subject='Email';
tsk.Status='In Progress';
tsk.WhatId=account1.Id;
listTasks.add(tsk);
List<user> toAdd=new List<user>();
list<string> toadd1 =new List<string>();
toAdd =([select user.email from user where user.id=:account1.Ownerid]);
for (user u: toadd){
toadd1.add(u.email);
}
mail.setToAddresses(toAdd1);
mail.setSubject('Attachments');
mail.setPlainTextBody('Test Message');
/*EmailTemplate emailt=[Select id from EmailTemplate where name = 'Account_Task_trigger'];
mail.setTargetObjectIds(listids);
mail.setSenderDisplayName('Vamshi Kothakapu SFDC');
mail.setTemplateId(emailt.id);
mail.setSaveAsActivity(false);*/
}}
else{ account1.adderror('another account exists with values of name&Industry');}
}
}insert listTasks;
Messaging.Email[] emails=new Messaging.Email[]{mail};
Messaging.sendEmail(emails); ////Error line 38 is here
}
}
I am a newbee here having problem to ececute a scenario from my institution.
When a new Account record is inserted check wheather any duplicate Account exists based on Account Name,Industry if duplicate record found throw error message. if duplicate records are notfound, check if type=Prospect, if so Create new Task for the owner of the account record and send email alert regarding new task. I have no idea ,I have put the toaddress but it is asking for receipent.
Above is the error when i tried to insert account. and my code with class and trigger are
Trigger:trigger AccTaskTrigr on Account (After insert ) { // Trigger code is good
AccTaskTrigr.AccountTaskTrigger(Trigger.New);}
class:
public class AccTaskTrigr {
public static void AccountTaskTrigger(List<Account> newtrigger){
List<Task> ListTasks = new List<Task>();
Task tsk =new task();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
list<account> accs =[select Name, Industry from Account];
For(account account1: newtrigger){
for(account a :accs){
if(a.name!=account1.name && a.Industry !=account1.Industry){
if( account1.Type=='Prospect'){
tsk.OwnerId=account1.OwnerId;
tsk.Priority='High';
tsk.Subject='Email';
tsk.Status='In Progress';
tsk.WhatId=account1.Id;
listTasks.add(tsk);
List<user> toAdd=new List<user>();
list<string> toadd1 =new List<string>();
toAdd =([select user.email from user where user.id=:account1.Ownerid]);
for (user u: toadd){
toadd1.add(u.email);
}
mail.setToAddresses(toAdd1);
mail.setSubject('Attachments');
mail.setPlainTextBody('Test Message');
/*EmailTemplate emailt=[Select id from EmailTemplate where name = 'Account_Task_trigger'];
mail.setTargetObjectIds(listids);
mail.setSenderDisplayName('Vamshi Kothakapu SFDC');
mail.setTemplateId(emailt.id);
mail.setSaveAsActivity(false);*/
}}
else{ account1.adderror('another account exists with values of name&Industry');}
}
}insert listTasks;
Messaging.Email[] emails=new Messaging.Email[]{mail};
Messaging.sendEmail(emails); ////Error line 38 is here
}
}
I found below issue in your code.
1) Need to add all account record in Query . Add filter in query and fatch only selected record. 2) Why for loop inside for loop. If you have 100 old record and you are inserting one new record then it will create muliple task record if you filter match
3) You can create workflow to send email on task creation. Simple create task and send email by workflow with the help of email alert.
4) SOQL inside for loop is not best pratice
Try to update your code like below
http://amitsalesforce.blogspot.com/2015/11/singleemailmessage-vs-massemailmessage.html
Let us know if this will help you
All Answers
Please may I request you to please refer the below link
https://salesforce.stackexchange.com/questions/118857/sendemail-failed-required-field-missing-missing-targetobjectid-with-template/118876#118876
I hope it will be helpful.
Best Regards
Rahul Kumar
thanks but not useful.
Please anyone help me get this done, check my code and hind me get it right.
I have modified the code amny times changing the if else loops, but in vain.
I found below issue in your code.
1) Need to add all account record in Query . Add filter in query and fatch only selected record. 2) Why for loop inside for loop. If you have 100 old record and you are inserting one new record then it will create muliple task record if you filter match
3) You can create workflow to send email on task creation. Simple create task and send email by workflow with the help of email alert.
4) SOQL inside for loop is not best pratice
Try to update your code like below
http://amitsalesforce.blogspot.com/2015/11/singleemailmessage-vs-massemailmessage.html
Let us know if this will help you
I found upper case and lower case were accepting as different accounts but I figured it out and fixed it. Thanks.