-1

this batch job does not work coz it does not update the record. As i am new to apex coding can anyone help me to add the update logic of the records? I am not sure where and how to add the update logic

global class BatchLeaseRenewalSubstatusUpdate implements Database.Batchable<sObject>, Schedulable{
    
     private ApplicationLogger logger = ApplicationLogger.getInstance();
     global Database.QueryLocator start(Database.BatchableContext BC) {
     Id leaseRenewalRecTypeId = 
           Schema.SObjectType.Lease_Renewal__c.getRecordTypeInfosByName().get('Lease 
           Renewal').getRecordTypeId();
        String statusVal = LeaseRenewals_Library.LR_STATUS_OPEN;
        string query = 'SELECT Id, Lease_Signed__c, Offer_Expiration__c, Yardi_Tenant_Status__c, Approved_Rent__c,'+ 
            +'Lease_to_Date__c, Renewal_Letter_Sent__c, Resident_Response__c,Made_Contact_With_Resident__c,'+
            +'Lease_Sent__c, Month_to_Month__c, Substatus__c, RecordTypeId, Status__c, On_MTM__c FROM Lease_Renewal__c '+
            +'where Status__c =: statusVal and RecordTypeId =: leaseRenewalRecTypeId';
        return Database.getQueryLocator(query);    
    }
    
    global void execute(Database.BatchableContext BC, List<Lease_Renewal__c> lrList) {
        
            LeaseRenewalStatusUpdateController.updateLeaseRenewalStatus(lrList);
        
        
    }
        
    global void finish(Database.BatchableContext BC) {
        System.debug(LoggingLevel.INFO, '==Batch Job Complete==');
    }
    
    global void execute(SchedulableContext SC){ }
}

this is the method where the logic is--

public static void updateLeaseRenewalStatus(List<Lease_Renewal__c> lstLeaseRenewals){
        for(Lease_Renewal__c objLeaseRenewal : lstLeaseRenewals){
                if(objLeaseRenewal.RecordTypeId == leaseRenewalRecTypeId){
                        if(objLeaseRenewal.Lease_Signed__c!=null){
                                objLeaseRenewal.Substatus__c = 'Renewed';
                        }else if(objLeaseRenewal.Yardi_Tenant_Status__c=='Past'){
                                objLeaseRenewal.Substatus__c = 'Moved Out';
                        }else if(objLeaseRenewal.Yardi_Tenant_Status__c=='Notice'){
                                objLeaseRenewal.Substatus__c = 'Notice';
                        }else{
                             objLeaseRenewal.Substatus__c =   'Status Error'; 
                        }
                }
Compo
  • 36,585
  • 5
  • 27
  • 39

1 Answers1

0

you just set the field values in memory but you don't actually save them to database.

Try

LeaseRenewalStatusUpdateController.updateLeaseRenewalStatus(lrList);
update lrList;
eyescream
  • 18,088
  • 2
  • 34
  • 46
  • I had tried that earlier but didnt work – Priya Mukherjee Sep 13 '22 at 12:00
  • what error you got? or did they not update, the last modified date stayed same? if you look at setup -> apex jobs did the batch actually run? how many "batches processed"? is your last function really empty body? it should be `Database.executeBatch(this);` or something like that – eyescream Sep 13 '22 at 14:44
  • I do not get any errors.. the records are just not updating as per the logic in 'updateLeaseRenewalStatus' method. My batch job runs nightly via scheduler. Its does run but since no update statement was present records were not getting updated in the database. Since this is my first batch job I am not sure how the code should be in order to update the records – Priya Mukherjee Sep 13 '22 at 14:56
  • Is `global void execute(SchedulableContext SC){ }` really empty body? Looks like you scheduled it to do nothing. You need Database.executeBatch() somewhere – eyescream Sep 13 '22 at 16:40
  • I am not sure now what should be in that method.. According to requirement my batch job should run nightly at 1:00 am .. But I dont know what should be included in global void execute(SchedulableContext SC){ } method – Priya Mukherjee Sep 13 '22 at 18:12
  • Database.executeBatch(this); – eyescream Sep 13 '22 at 18:27
  • Still not working – Priya Mukherjee Sep 13 '22 at 18:47
  • Any errors? What does it say in setup->apex jobs -> number of batches executed? Does it work when you execute it in console on the fly (`Database.executeBatch(new BatchLeaseRenewalSubstatusUpdate ());`)? If not - does the debug log say the start method returned 0 rows? – eyescream Sep 13 '22 at 21:23