Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global class BooksToAuthorsBatch implements Database.Batchable<SObject> {
- global BooksToAuthorsBatch() {
- }
- /**
- * @description gets invoked when the batch job starts
- * @param context contains the job ID
- * @returns the record set as a QueryLocator object that will be batched for execution
- */
- global Database.QueryLocator start(Database.BatchableContext context) {
- //return all books that don't have junction with writer and have author field filled
- return Database.getQueryLocator('SELECT Id, Name, Author__c FROM Book__c WHERE Id NOT IN (SELECT Book__c FROM BooksToWriters__c) AND Author__c != '' ');
- }
- /**
- * @description gets invoked when the batch job executes and operates on one batch of records. Contains or calls the main execution logic for the batch job.
- * @param context contains the job ID
- * @param scope contains the batch of records to process.
- */
- global void execute(Database.BatchableContext context, List<Book__c> scope) {
- Map<Id, List<Writer__c>> booksAndWriters = new Map<Id, List<Writer__c>>();
- Set<Writer__c> writers = new Set<Writer__c>();
- Map<Id, Set<String>> booksAndWritersNames = new Map<Id, Set<String>>();
- //get all writers names for each book
- Set<String> writersNames = new Set<String>();
- for(Book__c book : scope){
- List<String> splitedNames = book.Author__c.split(', ');
- writersNames.addAll(splitedNames);
- if(!booksAndWritersNames.contains(book){
- booksAndWritersNames.put(book, splitedNames);
- }
- else {
- booksAndWritersNames.get(book).put(splitedNames);
- }
- }
- //get all writers records that already in org and their names
- Set<String> writersExistNames = new Set<String>();
- for(Writer__c writer : [SELECT Id, Name FROM Writer__c WHERE Name IN :writersNames]){
- writers.add(writer);
- writersExistNames.add(writer.Name);
- }
- //create writers that doesn't exists
- List<Writer__c> newWritersToInsert = new List<Writer__c>();
- for(String writer : writersNames.removeAll(writersExistNames)){
- Writer__c newWriter = new Writer__c(Name = writer);
- newWritersToInsert.add(newWriter);
- writers.add(newWriter);
- }
- insert newWritersToInsert;
- List<BooksToWriters__c> booksToWritersToInsert = List<BooksToWriters__c>();
- for(Book__c book :booksAndWritersNames.keySet()){
- for(Writer__c writer : writers){
- if(booksAndWritersNames.get(book).contains(writer.Name)){
- booksToWritersToInsert.add(new BooksToWriters__c(Book__c = book, Writer__c = writer));
- }
- }
- }
- }
- /**
- * @description gets invoked when the batch job finishes. Place any clean up code in this method.
- * @param context contains the job ID
- */
- global void finish(Database.BatchableContext context) {
- Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
- mail.setToAddresses(new String[]{'sidoretsyura@gmail.com'});
- mail.setReplyTo('batch@acme.com');
- mail.setSenderDisplayName('Batch Processing');
- mail.setSubject('Batch Process Completed');
- mail.setPlainTextBody('Batch Process has completed');
- Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement