Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. global class BatchUpdate implements Database.Batchable<SObject>
  2. {
  3. global Database.QueryLocator start(Database.BatchableContext BC){
  4.  
  5. return Database.getQueryLocator([SELECT ContactId,Contact.Email,Contact.HasOptedOutOfEmail,Contact.custom__c,OldValue,NewValue FROM ContactHistory WHERE Field = 'custom__c' AND CreatedDate >= N_DAYS_AGO:10]);
  6. }
  7. global void execute (Database.BatchableContext BC, List<Contact> scope){
  8.  
  9. //Query to get all lists to map with contacts
  10. List<Lists__c> l1 = [Query List1];
  11.  
  12. List<Lists__c> l2 = [Query List2];
  13.  
  14. List<Lists__c> l3 = [Query List3];
  15. //
  16.  
  17. List<Item__c> ilist1 = new List<Item__c>();
  18. List<Item__c> ilist2 = new List<Item__c>();
  19. List<Item__c> ilist3 = new List<Item__c>();
  20.  
  21. Map<Id,Id> ctitem= new Map<Id,Id>();
  22. List<Id> pclist= new List<Id>();
  23.  
  24. //Map each contact with associated lists
  25.  
  26. for(ContactHistory ct: scope){
  27. if((ct.NewValue =='Test1' && ct.OldValue =='Test')||
  28. (ct.NewValue == 'Test2' && ct.OldValue =='Test')||
  29. (ct.NewValue =='Test3' && ct.OldValue =='Test')){
  30. for(Lists__c l : li){
  31. ctitem.put(ct.ContactId,l.id);
  32. ilisting.add(l.id);
  33.  
  34. }
  35. }
  36.  
  37. // Query list of items to get if the combination of contact and List are already in the Item
  38.  
  39. List<Item__C> ilistnn = [select id, Contact__c, Lists__c from Item__c where Contact__c =:ctitem.keyset()];
  40. }
  41. for(Id keyId : ctitem.keyset()){
  42. for(Id pf : ilisting){
  43.  
  44. //Add the rest of the lists that are associated with the contact
  45.  
  46. if(!ctitem.keyset.contains(pf)){
  47.  
  48. Item__C ic1 = new Item__c();
  49. pc1.Contact__c = keyId;
  50. pc1.Lists__c = pf;
  51. pclist.add(pc1);
  52. }
  53. }
  54. global void finish(Database.BatchableContext BC){
  55. }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement