function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ramana123Ramana123 

Create a field called LiveDate(Date field) on contact. Write a batch to get the contacts whose LiveDate is today and send those contact details to the currently logged in user email .

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){
       }
    }
ArleneArlene
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();