Skip to main content
Olga Kim (Olga Kim) ha fatto una domanda in #Apex
I have a requirement to create a batch class for Tenant__c object for the Property management app. 

The condition is when our Tenant doesn't pay rent by the beginning of the month, we send him/her an email with a reminder.  

There is a field called "Status" on Tenant object if the status is not "Paid" then send email.

I wrote a batch class but I am not sure where do I put condition to send emails? In Execute or Finish?

Please Help!

global class TenantLatePaymentTask implements Database.Batchable<SObject> {

         global Database.QueryLocator start(Database.BatchableContext BC) {

            return Database.getQueryLocator([

        SELECT Name, Id, Email_Address__c,description__c from Tenant__c

        Where Status__c='Non-Active']); }

global void execute(Database.BatchableContext BC, List<Tenant__c> tenants) {

list<Tenant__c> TenList=new list<Tenant__c>();

   

    for (Tenant__c tenant : tenants) {

tenant.description__c='Last Payment is missing. Please Follow up with the Tenant';

TenList.add(tenant);          

        

}

    list<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

    for (Tenant__c Ten :TenList ) {

    if (Ten.Email_Address__c != null && Ten.Status__c!='Paid') {

      

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

     

      List<String> sendTo = new List<String>();

      sendTo.add(Ten.Email_Address__c);

      mail.setToAddresses(sendTo);

    

     

      mail.setReplyTo('dreamhouse@gmail.com');

      mail.setSenderDisplayName('Dreamhouse real estate Co');

    

     

      List<String> ccTo = new List<String>();

      ccTo.add('business@bankofnigeria.com');

      mail.setCcAddresses(ccTo);

      

      mail.setSubject('Last Rent Payment is Missing');

      String body = 'Dear ' + Ten.First_Name__c + ', ';

      body += 'Please, submit your last rent payment';

      mail.setHtmlBody(body);

    

      

      mails.add(mail);

    }

  }

  

  Messaging.sendEmail(mails);

    if(TenList!=null && TenList.size()>0){

        database.update(TenList);

}

}global void finish(Database.BatchableContext BC) {

 

}

 
2 risposte
0/9000