Advertisement
YuraSidorets

Untitled

Aug 7th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.13 KB | None | 0 0
  1. global class BooksToAuthorsBatch implements Database.Batchable<SObject> {
  2.    
  3.     global BooksToAuthorsBatch() {
  4.        
  5.     }
  6.    
  7.     /**
  8.      * @description gets invoked when the batch job starts
  9.      * @param context contains the job ID
  10.      * @returns the record set as a QueryLocator object that will be batched for execution
  11.      */
  12.     global Database.QueryLocator start(Database.BatchableContext context) {
  13.         //return all books that don't have junction with writer and have author field filled
  14.         return Database.getQueryLocator('SELECT Id, Name,  Author__c FROM Book__c WHERE Id NOT IN (SELECT Book__c FROM BooksToWriters__c) AND Author__c != '' ');
  15.     }
  16.  
  17.     /**
  18.      * @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.
  19.      * @param context contains the job ID
  20.      * @param scope contains the batch of records to process.
  21.      */
  22.     global void execute(Database.BatchableContext context, List<Book__c> scope) {
  23.         Map<Id, List<Writer__c>> booksAndWriters = new Map<Id, List<Writer__c>>();
  24.         Set<Writer__c> writers = new Set<Writer__c>();
  25.        
  26.         Map<Id, Set<String>> booksAndWritersNames = new Map<Id, Set<String>>();
  27.  
  28.         //get all writers names for each book
  29.         Set<String> writersNames = new Set<String>();
  30.         for(Book__c book : scope){
  31.             List<String> splitedNames = book.Author__c.split(', ');
  32.             writersNames.addAll(splitedNames);
  33.            
  34.             if(!booksAndWritersNames.contains(book){
  35.                 booksAndWritersNames.put(book, splitedNames);
  36.             }
  37.             else {
  38.                 booksAndWritersNames.get(book).put(splitedNames);
  39.             }
  40.         }
  41.  
  42.         //get all writers records that already in org and their names
  43.         Set<String> writersExistNames = new Set<String>();
  44.         for(Writer__c  writer : [SELECT Id, Name FROM Writer__c WHERE Name IN :writersNames]){
  45.             writers.add(writer);
  46.             writersExistNames.add(writer.Name);
  47.         }
  48.  
  49.         //create writers that doesn't exists
  50.         List<Writer__c> newWritersToInsert = new List<Writer__c>();
  51.         for(String writer : writersNames.removeAll(writersExistNames)){
  52.             Writer__c newWriter = new Writer__c(Name = writer);
  53.             newWritersToInsert.add(newWriter);
  54.             writers.add(newWriter);
  55.         }
  56.         insert newWritersToInsert;
  57.        
  58.         List<BooksToWriters__c> booksToWritersToInsert = List<BooksToWriters__c>();
  59.         for(Book__c book :booksAndWritersNames.keySet()){
  60.             for(Writer__c writer : writers){
  61.                 if(booksAndWritersNames.get(book).contains(writer.Name)){
  62.                     booksToWritersToInsert.add(new BooksToWriters__c(Book__c = book, Writer__c = writer));
  63.                 }
  64.             }
  65.         }
  66.     }
  67.    
  68.     /**
  69.      * @description gets invoked when the batch job finishes. Place any clean up code in this method.
  70.      * @param context contains the job ID
  71.      */
  72.     global void finish(Database.BatchableContext context) {
  73.         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  74.         mail.setToAddresses(new String[]{'sidoretsyura@gmail.com'});
  75.         mail.setReplyTo('batch@acme.com');
  76.         mail.setSenderDisplayName('Batch Processing');
  77.          mail.setSubject('Batch Process Completed');
  78.          mail.setPlainTextBody('Batch Process has completed');
  79.          Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  80.  
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement