Priya Mukherjee
Priya Mukherjee

Reputation: 1

How to update records from a batch job in Apex?

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'; 
                        }
                }

Upvotes: -1

Views: 830

Answers (1)

eyescream
eyescream

Reputation: 19637

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

Try

LeaseRenewalStatusUpdateController.updateLeaseRenewalStatus(lrList);
update lrList;

Upvotes: 0

Related Questions