Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global class BatchUpdate implements Database.Batchable<SObject>
- {
- global Database.QueryLocator start(Database.BatchableContext BC){
- 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]);
- }
- global void execute (Database.BatchableContext BC, List<Contact> scope){
- //Query to get all lists to map with contacts
- List<Lists__c> l1 = [Query List1];
- List<Lists__c> l2 = [Query List2];
- List<Lists__c> l3 = [Query List3];
- //
- List<Item__c> ilist1 = new List<Item__c>();
- List<Item__c> ilist2 = new List<Item__c>();
- List<Item__c> ilist3 = new List<Item__c>();
- Map<Id,Id> ctitem= new Map<Id,Id>();
- List<Id> pclist= new List<Id>();
- //Map each contact with associated lists
- for(ContactHistory ct: scope){
- if((ct.NewValue =='Test1' && ct.OldValue =='Test')||
- (ct.NewValue == 'Test2' && ct.OldValue =='Test')||
- (ct.NewValue =='Test3' && ct.OldValue =='Test')){
- for(Lists__c l : li){
- ctitem.put(ct.ContactId,l.id);
- ilisting.add(l.id);
- }
- }
- // Query list of items to get if the combination of contact and List are already in the Item
- List<Item__C> ilistnn = [select id, Contact__c, Lists__c from Item__c where Contact__c =:ctitem.keyset()];
- }
- for(Id keyId : ctitem.keyset()){
- for(Id pf : ilisting){
- //Add the rest of the lists that are associated with the contact
- if(!ctitem.keyset.contains(pf)){
- Item__C ic1 = new Item__c();
- pc1.Contact__c = keyId;
- pc1.Lists__c = pf;
- pclist.add(pc1);
- }
- }
- global void finish(Database.BatchableContext BC){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement