You need to sign in to do that
Don't have an account?
NOT RECIEVE EMAIL IN USER MAIL ID using BATCH APEX
I have 2 users both have admin licience,,,Now Those account Whose indust is not null there Owner is User2.
As per my batch class At the end User should get some mail,I am not getting any email.
When i check Job its shown as completed.
BELOW CODE(JUST COPY AND PASTE AND CHECK):
global class Batch_Apex_Example_2 implements Database.Batchable<sobject>,Database.Stateful{
public Map<Id,List<String>> accMap;
global Batch_Apex_Example_2(){
accMap=new Map<Id,List<String>>();
}
global Database.QueryLocator start(Database.BatchableContext bc){
String query='Select id,name,ownerId from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc,List<Account> scope){
for(Account a:scope){
if(accMap.containsKey(a.ownerId)){
List<String> names=accMap.get(a.ownerId);
names.add(a.name);
accMap.put(a.ownerId,names);
}else{
List<String> names=new List<String>();
names.add(a.name);
accMap.put(a.ownerId,names);
}
}
}
global void finish(Database.BatchableContext bc){
Set<Id> keys=accMap.keySet();
List<User> users=[Select id,email from user where id in:keys];
Map<Id,String> userMap=new Map<Id,String>();
for(User u:users){
userMap.put(u.id,u.email);
}
System.debug('userMap===========>'+userMap);
List<Messaging.Email> emails=new List<Messaging.Email>();
for(Id k:keys){
Messaging.SingleEmailMessage msg=new Messaging.SingleEmailMessage();
List<String> accounts=accMap.get(k);
String body='';
for(String a:accounts){
body=body+'<br/>'+a;
}
msg.setSubject('Your Accounts');
msg.setHtmlBody(body);
String email=userMap.get(k);
String[] toadd=new String[]{email};
msg.setToAddresses(toadd);
emails.add(msg);
}
Messaging.sendEmail(emails);
}
}
/*FOR EXECUTING BATCH CLASS
1)cltr+E
2)Batch_Apex_Example_2 be=new Batch_Apex_Example_2();
Id jobId=Database.executeBatch(be,5);
3)Execute
*/
As per my batch class At the end User should get some mail,I am not getting any email.
When i check Job its shown as completed.
BELOW CODE(JUST COPY AND PASTE AND CHECK):
global class Batch_Apex_Example_2 implements Database.Batchable<sobject>,Database.Stateful{
public Map<Id,List<String>> accMap;
global Batch_Apex_Example_2(){
accMap=new Map<Id,List<String>>();
}
global Database.QueryLocator start(Database.BatchableContext bc){
String query='Select id,name,ownerId from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc,List<Account> scope){
for(Account a:scope){
if(accMap.containsKey(a.ownerId)){
List<String> names=accMap.get(a.ownerId);
names.add(a.name);
accMap.put(a.ownerId,names);
}else{
List<String> names=new List<String>();
names.add(a.name);
accMap.put(a.ownerId,names);
}
}
}
global void finish(Database.BatchableContext bc){
Set<Id> keys=accMap.keySet();
List<User> users=[Select id,email from user where id in:keys];
Map<Id,String> userMap=new Map<Id,String>();
for(User u:users){
userMap.put(u.id,u.email);
}
System.debug('userMap===========>'+userMap);
List<Messaging.Email> emails=new List<Messaging.Email>();
for(Id k:keys){
Messaging.SingleEmailMessage msg=new Messaging.SingleEmailMessage();
List<String> accounts=accMap.get(k);
String body='';
for(String a:accounts){
body=body+'<br/>'+a;
}
msg.setSubject('Your Accounts');
msg.setHtmlBody(body);
String email=userMap.get(k);
String[] toadd=new String[]{email};
msg.setToAddresses(toadd);
emails.add(msg);
}
Messaging.sendEmail(emails);
}
}
/*FOR EXECUTING BATCH CLASS
1)cltr+E
2)Batch_Apex_Example_2 be=new Batch_Apex_Example_2();
Id jobId=Database.executeBatch(be,5);
3)Execute
*/
Please find the below code.
Replace the xxxx with your userid.
Thanks,
All Answers
I tried to run the same batch in my personal org and i did not received the email at first. Then i found there are some account which were created by automated process user .
As the email in that automated process is invalid so the batch is failing.To fix this issue only query the account which were created by your users by using
After making the change i received the email. Hope the same might me issue with your org as well.
If this solution helps , Please mark it as best answer.
Thanks,
Please find the below code.
Replace the xxxx with your userid.
Thanks,