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

getiing error in trigger REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc):
I want a trigger in account object when a new record is inserted with same phon enumber then it will automatically mail to account owner that account has been ctreated .
but i am facing issue REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc):
below is my code
trigger EmailAfterInserttest on Account(after Insert,after update) {
Messaging.reserveSingleEmailCapacity(trigger.size);
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Set<Id> accId = new Set<Id>();
List<ID>ownerids=new List<ID>();
List<String> sendTo = new List<String>();
List<User>users=[select name,id,email from user where id in:ownerids];
for(User u:users){
sendTo.add(u.Email);
}
for(Account a : Trigger.new)
{
accId.add(a.Id);
ownerids.add(a.ownerid) ;
}
for (Account acct : Trigger.new) {
List<Account> accList = [Select ID, owner.Id,owner.email, Name ,fax, phone From Account Where Id in :accId And phone=:acct.phone];
if(acclist.size()>0)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(sendTo);
email.setSubject('Inserted Account Alert');
email.setPlainTextBody('This message is to alert you that the account named ' + acct.Name + ' has been inserted.');
emails.add(email);
Messaging.sendEmail(emails);
}
}
}
but i am facing issue REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc):
below is my code
trigger EmailAfterInserttest on Account(after Insert,after update) {
Messaging.reserveSingleEmailCapacity(trigger.size);
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Set<Id> accId = new Set<Id>();
List<ID>ownerids=new List<ID>();
List<String> sendTo = new List<String>();
List<User>users=[select name,id,email from user where id in:ownerids];
for(User u:users){
sendTo.add(u.Email);
}
for(Account a : Trigger.new)
{
accId.add(a.Id);
ownerids.add(a.ownerid) ;
}
for (Account acct : Trigger.new) {
List<Account> accList = [Select ID, owner.Id,owner.email, Name ,fax, phone From Account Where Id in :accId And phone=:acct.phone];
if(acclist.size()>0)
{
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(sendTo);
email.setSubject('Inserted Account Alert');
email.setPlainTextBody('This message is to alert you that the account named ' + acct.Name + ' has been inserted.');
emails.add(email);
Messaging.sendEmail(emails);
}
}
}
Also, your trigger isn't bulkified - you have a query inside a for loop is one example. This means that if you bulk insert accounts it stands a good chance of failiing. You can read up on bulkification at:
https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
you got this error message because your sendTo list is empty use below code
* first fill ownerids list and then you can query this users.
i hope it helps you
thanks let me inform if it helps you