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

Batch Apex email Scheduling
Hi
when ever a record inserted by batch apex mail should be send to the respective owner how to achieve this
Thanks in advance
when ever a record inserted by batch apex mail should be send to the respective owner how to achieve this
Thanks in advance
you can follow below code:
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
You can take reference from this below code:-
In case you find any other issue please mention.
If you find your Solution then mark this as the best answer.
below is my code
Global class BatchEmail implements Database.Batchable<SObject>
{
Global Database.QueryLocator Start(Database.BatchableContext bContext)
{
string accountsQuery = 'Select id, name, annualrevenue, fax, customerpriority__C, active__C from Account';
return Database.getQueryLocator(accountsQuery);
}
Global void Execute(Database.BatchableContext bContext, List<SObject> recordsToProcess)
{
if(! recordsToProcess.isEmpty())
{
List<Account> accountsToUpdate = new List<Account>();
for(SObject obj : recordsToProcess)
{
Account acc = (Account) obj;
acc.AnnualRevenue = 6000000;
acc.Fax = '99998888';
acc.CustomerPriority__c = 'High';
acc.Active__c = 'Yes';
accountsToUpdate.Add(acc);
}
if(! accountsToUpdate.isEmpty())
{
Update accountsToUpdate;
}
}
}
Global void Finish(Database.BatchableContext bContext)
{
EmailTemplate templateId = [Select Id from EmailTemplate where DeveloperName = 'Testing'];
List<account> lstAccount = new list<account>();
for(Account acc : lstAccount)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(acc.id);
mail.setTreatTargetObjectAsRecipient(false);
// mail.setToAddresses(new String[] {ta.Owner.mail});
mail.setTemplateID(templateId.Id);
mail.setSaveAsActivity(false);
lstAccount.add(acc);
insert lstAccount;
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
}
But i could not able to achieve my usecase
(after updating records mails should be send to the respective owner of the record ( i have the email templete by the name testing))
Any assistance
Thanks in advance
Do you want to update account or insert account.? because in FINISH method lstAccount list is empty that's why loop will not execute.
Please give me your requirment what exactly you want do
Regards
Mukesh
I have two requirements insert and update in both the cases mail should be sent to the respective owner of the record.
In the above code what changes should i do to run succesfully
Regrads
Teja
Please follow below code
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Thanks for your response
i am getting these errors when trying to use your above code
Variable does not exist: batchList (this.batchList = newAccList;)
Method does not exist or incorrect signature: void getQueryLocator(String) from the type DataBase (return Database.getQueryLocator(accountsQuery);)
Invalid loop variable type expected SObject was Account ( for(Account acc : accountsToInsert)
Variable does not exist: mail1 (Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail1});)
Expecting '}' but was: 'for' (for(Account acc : accountsToUpdate))
errors and corresponding code