Skip to main content
i wrote below code can anyone help me in this plzz., it is  ot working

global class Live_Date_Batch_Class implements Database.Batchable<sObject>{

            

            public List<Contact> conList {get;set;}

    

    global Live_Date_Batch_Class()

        {

           

        }

        global Database.QueryLocator start(Database.BatchableContext BC)

        {

            string query = 'select id,LastName from Contact WHERE Live_Date__c = TODAY';

            System.debug('aaaaaaaaaaaaaaaaaaaaaaaa'+query) ; 

            return Database.getQueryLocator(query);

        }

       global void execute(Database.BatchableContext BC, List<Contact>scope)

       {

            Set<Id> convertedIdSet = new Set<Id>();

            List<messaging.SingleEmailMessage>  emails = new List<messaging.SingleEmailMessage>();

            for(Contact contact : scope) 

       {

            convertedIdSet.add(contact.Id);

       }    

       

                   for(Contact contact : scope) 

       {

            conList = ([SELECT FirstName, LastName, Email, Phone, Account.Id

                        FROM Contact 

                        WHERE Id IN :convertedIdSet] );

  

       }    

           String userName = UserInfo.getUserName();

            User activeUser = [Select Email From User where Username = : userName limit 1];

            String userEmail = activeUser.Email;

           

           

       System.debug('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'+convertedIdSet) ; 

       Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

       email.setToAddresses(new String[] {userEmail});

       email.setSubject('+convertedIdSet+'); 

       email.setPlainTextBody('details');

       emails.add(email);

       String body = 'Contact Details : ' +conList+ ' ';

       Messaging.sendEmail(emails);    

       }

            

       global void finish(Database.BatchableContext BC){

       }

    }
1 个回答
  1. 2020年6月2日 21:55
    Not sure what isn't working for you, but I have a suggestion.  If you have more than 200 contacts, or the number of found contacts exceeds your batch size, this probably won't do what you expect.  I would recommend that extend Database.Stateful on your class definition.  This will allow conlist (which doesn't neet {get; set;}) to be retained between batches.  Each batch would add to the list, rather than rebuild it.  Then, send your email in your finish method.

    If you select FirstName, LastName, Email, Phone, Account.Id in your QueryLocator, you won't need to do a second query in your execute method.  You would only add to the list you are keeping.  Finally, you can build your id set in the finish method with

    Map<Id,Contact> contacts = new Map<Id,Contact>(conList);

    Set<Id> convertedIdSet = contacts.keySet();

     
0/9000