0

When I run the bacth class below through Developer COnsole, I see that the batch class generates 100's of emails though the specify batch size as 1. Can some one help me with this

global class BatchEmail implements Database.Batchable<SObject>
{

    global Database.QueryLocator start(Database.BatchableContext BC){
    return Database.getQueryLocator([
            SELECT Id,Contact__c,Group__c FROM Member__c WHERE CreatedDate = N_DAYS_AGO:1 and Contact__r.Date__c>=2017-08-31
        ]);
    }
        Map<Id,String> contact = new Map<Id,String>();
        Set<Id> Student = new Set<Id>();
        Set<Id> sol = new Set <Id>(); 
        public Map<Id,Id> Comm = new Map <Id,Id>();
        List<Id> CId = new List<Id>();
        Map<Id, Id> getUser = new Map<Id, Id>();
        List<Line__c> Rep = new List<Line__c>();

    global void execute(Database.BatchableContext BC, List<Member__c> scope){
        system.debug('scope'+scope.size());         

       for(Line__c l : [Select Receipt__r.Contact__c,
                                              Receipt__r.Contact__r.Type__c,Receipt__r.Contact__r.Catgory__c,
                                            Is_Renew__c,Receipt__r.Contact__r.Id, Id From Line__c where CreatedDate = N_DAYS_AGO:1]){
            system.debug('create list');
            CId.add(l.Receipt__r.Contact__c);  
            Rep.add(l);
        }
        for(User U : [Select u.ContactId, u.Id From User u where u.ContactId IN : CId]){
            getUser.put(U.ContactId,U.Id);          
        }
        for(Line__c l : Rep){               

                if(l.Is_Renew__c == False){
                    contact.put(l.Receipt__r.Contact__c, l.Receipt__r.Contact__r.Type__c);
                                                          }
        }
        for(Contact pContact : [Select Id,Type__c,Catgory__c,Date__c From 
                                      Contact where Id IN : contact.keyset() and Join_Date__c >=2017-08-31]){ 
                                            if(contact.get(pContact.Id) == 'xxx' && pContact.Catgory__c == 'xxx'){
                                            sol.add(pContact.Id);
                                            }else if (contact.get(pContact.Id) =='xxx'){
                                                Student.add(pContact.Id);
                                            }

          for (Member__c m : [Select Id,Contact__c,Group__c from Member__c where CreatedDate = N_DAYS_AGO:1]){
                                if (m!=null){
                                comm.put(pContact.Id,m.Group__c);   
                                }                                                                               
                            } 

                                      }
    String urlInstance = String.valueof(System.URL.getSalesforceBaseURL());
                                 List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
                                 List<String> email = new List<String>();                               
                                 EmailTemplate templateId = [Select id from EmailTemplate where name = 'xxx'];

                                 for (Id keyvalue : lComm.keyset())
                                 {  
                                 OrgWideEmailAddress owa = [select id, DisplayName, Address from OrgWideEmailAddress limit 1];
                                 for(Group__c Group : [Select Id, (Select c.Member__r.Email From Committs__r c where c.Date__c <= TODAY and c.Thrugh_Date__c >= TODAY  and c.Code__c IN ('X','N','L','LPP')) From Group__c g
                                                    where Id = : lComm.get(keyvalue)]){

                                    if(!Student.contains(keyvalue)){
                                            if(!sol.contains(keyvalue)){

                                            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                                            for(Committe__c comt : Group.Commites__r){
                                                email.add(comt.Member__r.Email);
                                            }
                                            system.debug(email.size());
                                            mail.setToAddresses(email);
                                            mail.setOrgWideEmailAddressId(owa.id);
                                            mail.setTargetObjectId(keyvalue);
                                            mail.setTemplateID(templateId.Id); 
                                            mail.setSaveAsActivity(false); 
                                            mails.add(mail);


                                    }
                                                    }
                            }         
                                 Messaging.sendEmail(mails);


                                 }
    }
    global void finish(Database.BatchableContext BC)
    {
    }
}
  • Depending on how many rows you iterate through, that makes sense. Batch size of one means you will be processing one Member__c record at a time and, from what I can see, you're sending an email at the end of every batch. If you have 100s of members, you will get 100s of emails. – Sebastian Kessel Aug 9 '17 at 15:31
  • But for 1 member record processing, I am receiving 10-15 emails – user46620 Aug 9 '17 at 15:32
  • The batch number in this case has nothing to do with your problem, then. I see the mails.add(mail); is inside of a for-loop. There must be a logic problem in your code. I don't have the context to debug it for you. – Sebastian Kessel Aug 9 '17 at 15:35
  • Sebastian, If I remove that statement out of the loop, there is no email that I could receive. – user46620 Aug 9 '17 at 15:43
  • I believe, that Member__c is queried twice and that might be causing the issue. In the for loop, I had the query. – user46620 Aug 9 '17 at 15:44
0

First thought: Looking over the loops around the mailing section, I see lComm been looped through but I don't see it being defined anywhere.

My logic is seeing that since you're using a keyset to define "keyvalue" then it can't be referenced more than once (because of how sets work) but you're looping through all Committe__c records under the matching group. Is it possible that one Member__c is referenced in more than one Committe__c, which would make them get duplicate emails but they're not really dupes?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.