How to write a batch apex code to fetch all account details, creating related contacts record for each account and after finishing setting checkbox to false, sending email about the status along with its test class
Hii Aditi Mohanty Try The Following Code This Will Create Contacts For Every Account That Does Not Have Contact
global class Batch_AddConToAcc implements Database.Batchable <sObject> {
List<contact> lstCon = new List<Contact>();
global Database.QueryLocator start(Database.BatchableContext bc) {
String query = 'SELECT Id, Name FROM Account WHERE Id NOT IN(SELECT AccountId FROM Contact)';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc,List<Account> batch) {
for (Account a : batch) {
Contact c = new Contact();
c.LastName = a.Name;
c.AccountId = a.Id;
lstCon.add(c);
}
INSERT lstCon;
}
global void finish(Database.BatchableContext bc) {
//Do Nothing.
}
}
Try The Following Code
This Will Create Contacts For Every Account That Does Not Have Contact Please Mark it As Best if it Helps
Can you help me in the email part and the test class part. After the finish() part, how to change checkbox to false?
Sorry But Email Part I Don't Know How To Implement It
and The ChekBox Field Is in Account Or Contact Please Mark it As Best if it Helps