0

i m trying to write the query in outside of for loop . but yet i have no idea howi can do modify code . below code how i write the account query Account (usernmae = [Select id, KAM__r.email ,TL_for_Integration__r.email,SAM_for_Integration__r.email from Account where id =: opp.AccountId];) the outside the for loop.

public class OpportunityEmailNotification implements Database.Batchable<SObject> {


    public void execute(SchedulableContext context) {
        Database.executeBatch(this);
    }
    public Database.QueryLocator start(Database.BatchableContext context) {

        return Database.getQueryLocator( [SELECT Id,name,ownerid,accountID ,Channel_Partner_KAM_Email__c,Channel_Partner_Manager__c,CloseDate FROM opportunity  where Status__c =:'Open']);

        }

    public void execute(Database.BatchableContext context, list<opportunity> records) {
         id t;  
         id s;
         String KAMEmail ;
         String ChannelPartnerEmail ;
         date oport;  

         Contact c = [select id, Email from Contact where email <> null limit 1];     
         EmailTemplate em =[SELECT id FROM EmailTemplate WHERE Name = 'CP - Opportunity Expected Closure date Alert'];
         system.debug('TTTTTTTTTTTTT'+em );
         system.debug('AAAAAAAAAAAAA'+records);


       // list<id> oppid = new list<id>();   


         for(opportunity  opp :records){
           t= opp.id;
           s= opp.AccountId;  
           KAMEmail =opp.Channel_Partner_KAM_Email__c;
           ChannelPartnerEmail =opp.Channel_Partner_Manager__c ;
           oport = opp.CloseDate ;     
            **Account usernmae = [Select id,  KAM__r.email ,TL_for_Integration__r.email,SAM_for_Integration__r.email from Account where id =: opp.AccountId];**

           system.debug('SSSSSSSSSSSS'+usernmae );     
           if(Date.Today() == oport-1|| Date.Today() ==oport-3 || Date.Today()==oport-5){  
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           mail.setTemplateId(em.id);
           mail.setwhatid(t);   
           mail.setTargetObjectId(c.id);
           List<String> ccTo = new List<String>();   
           ccTo.add(usernmae.KAM__r.email);
           ccTo.add(usernmae.SAM_for_Integration__r.email);
           system.debug('QQQqqqqq'+ccTo);
          ccTo.add(usernmae.TL_for_Integration__r.email);
          system.debug('NNNNNNNNNNNNNNNNNNNNNN'+ccTo);

           mail.setCcAddresses(ccTo);       
           mail.setSaveAsActivity(false);
           try{
           Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
           system.debug('ZZZZZZZZZZZ'+mail);          

           }     
        catch (exception e){     
          system.debug('Exception occured in SendEmail'+mail);
        }
       }
     }
        // Update records, send notifications here   
    }
    public void finish(Database.BatchableContext context) {
        // No need to do anything, but this needs to be here
    }   
 }

1 Answer 1

1

Simple, Change your query locator from:

return Database.getQueryLocator( [SELECT Id,name,ownerid,accountID ,Channel_Partner_KAM_Email__c,Channel_Partner_Manager__c,CloseDate FROM opportunity  where Status__c =:'Open']);

to

return Database.getQueryLocator( [SELECT Id,name,ownerid,accountID,
         Channel_Partner_KAM_Email__c,Channel_Partner_Manager__c,CloseDate,
         Account.KAM__r.email, 
         Account.TL_for_Integration__r.email,
         Account.SAM_for_Integration__r.email 
         FROM opportunity  where Status__c =:'Open']);

Also this will cause you problems in the future Since it is non-selective:

Contact c = [select id, Email from Contact where email <> null limit 1];

Not to mention what could you possible be doing with this one random contact in a batch class???

4
  • Contact c = [select id, Email from Contact where email <> null limit 1]; using for mail.setTargetObjectId(c.id); . mail .setTargetobjectId will using the whatid Aug 11, 2015 at 5:30
  • But why would you ever want to send a bunch of emails via batch to a random unknown contact???? Not to mention this could get your business in trouble
    – Eric
    Aug 11, 2015 at 5:48
  • how we can avoid this. because if we are using Template then setTargetObjectID is required . Aug 11, 2015 at 5:51
  • I do not know, depends on your requirements but maybe set it to the person you are actually sending the email to or the ?
    – Eric
    Aug 11, 2015 at 5:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .