You need to sign in to do that
Don't have an account?
Kunal Purohit 4
How to write batch apex code for following scenario
I want to send mail with Total_Paper_Published value. Total_Paper_Published__c is a rollup summary field.
Here is the code. Plz suggest correction.
public class EmailBatch implements database.Batchable<sobject>,Schedulable{
public decimal count=0;
public void execute(Schedulablecontext sc)
{
EmailBatch eb=new EmailBatch();
database.executeBatch(eb);
}
public database.QueryLocator start(database.BatchableContext bc)
{
string query='select Name,Journal_Email_Address__c,Total_Published_Paper__c from Account';
return database.getQueryLocator(query);
}
public void execute(database.BatchableContext bc,list<Account> acc)
{
list<Account>alist=new list<Account>();
for(Account a:acc)
{
count=a.Total_Published_Paper__c;
}
list<Messaging.SingleEmailMessage> mlist=new list<Messaging.SingleEmailMessage>();
messaging.SingleEmailMessage email=new messaging.SingleEmailMessage();
email.setSubject('Welcome');
email.setPlainTextBody('Total Paper Published' +count);
string[] to=new string[]{'kunalpurohit7@gmail.com'};
email.setToAddresses(to);
mlist.add(email);
messaging.SendEmail(mlist);
system.debug('Mail sent Successfully');
}
public void finish(database.BatchableContext bc)
{
//system.debug(count);
}
}
Here is the code. Plz suggest correction.
public class EmailBatch implements database.Batchable<sobject>,Schedulable{
public decimal count=0;
public void execute(Schedulablecontext sc)
{
EmailBatch eb=new EmailBatch();
database.executeBatch(eb);
}
public database.QueryLocator start(database.BatchableContext bc)
{
string query='select Name,Journal_Email_Address__c,Total_Published_Paper__c from Account';
return database.getQueryLocator(query);
}
public void execute(database.BatchableContext bc,list<Account> acc)
{
list<Account>alist=new list<Account>();
for(Account a:acc)
{
count=a.Total_Published_Paper__c;
}
list<Messaging.SingleEmailMessage> mlist=new list<Messaging.SingleEmailMessage>();
messaging.SingleEmailMessage email=new messaging.SingleEmailMessage();
email.setSubject('Welcome');
email.setPlainTextBody('Total Paper Published' +count);
string[] to=new string[]{'kunalpurohit7@gmail.com'};
email.setToAddresses(to);
mlist.add(email);
messaging.SendEmail(mlist);
system.debug('Mail sent Successfully');
}
public void finish(database.BatchableContext bc)
{
//system.debug(count);
}
}
Can you try the below code:
For executing the batch class you can use the below lines in anonymous window:
Additionally, I would suggest you have a look at the below trailhead module for information and in case if you want to make sure that this run at a scheduled interval then you can use the above lines in a schedulable apex implementation and make sure it runs as per the time mentioned.
Let me know if there is any need for help.
Regards,
Anutej