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
sgsg 

Logic to check if the submitted date/time is after 2PM and skip the update of that record- Batch class

This is the sample batch class that I am working on. This class is working fine but I need to add one more condition like, If status is set to submitted after 2PM TODAY, The number field should not be increased by Today Batch Job which runs everyday at 3PM, So that record should be increased in next day run. How to achieve that? Any help would be appreciated. TIA.

global class BatchCounter implements Database.Batchable<sObject> {
static final String businessHoursId;
global Database.QueryLocator start(Database.BatchableContext bc) {
String query = 'SELECT Id, numberField__c,statusField__c,Submitteddate__c FROM objectName__c WHERE Submitteddate__c !=null '+' AND statusField__c IN (\'Assigned\', \'Submitted\', \'InProgress\')'; return Database.getQueryLocator(query);
} global void execute(Database.BatchableContext bc, List<TMA__c> tmas)
{
for (TMA__c tma : tmas) {
if (isBusinessHours(businessHoursId))
{ tma.numberField__c+=1;
}}
update tmas;
}
global void finish(Database.BatchableContext bc) { }
private static Boolean isBusinessHours(Id businessHoursId) { BusinessHours businessHours = [SELECT Id FROM BusinessHours WHERE Id = :businessHoursId];
if (businessHours != null)
{ Datetime currentDatetime = Datetime.now();
return BusinessHours.isWithin(businessHoursId, currentDatetime);
}
return false;
} }
SwethaSwetha (Salesforce Developers) 
HI Sg,
You could alter the execute method to check if the record was submitted after 2 PM today. If the record's status is set to submitted after 2 PM today, the batch job will not increment its number field. Instead, it will update the record's submitted date to be tomorrow. The record will then be picked up by the batch job the next day, and its number field will be incremented at that time.

Something like:
global void execute(Database.BatchableContext bc, List<TMA__c> tmas)
{
    for (TMA__c tma : tmas) {
        if (isBusinessHours(businessHoursId)) {
            // Check if record was submitted after 2 PM today
            Datetime today2PM = Datetime.newInstance(Date.today(), Time.newInstance(14, 0, 0));
            if (tma.Submitteddate__c != null && tma.Submitteddate__c >= today2PM) {
                // Set Submitteddate__c to tomorrow
                tma.Submitteddate__c = tma.Submitteddate__c.addDays(1);
            } else {
                // Increment numberField__c
                tma.numberField__c += 1;
            }
        }
    }
    update tmas;
}

If this information helps, please mark the answer as best. Thank you
sgsg
Hi Swetha,

 I am not supposed to do that, Because Its under the control of Business and I cannot change that. Their business hours is 8 AM to 5PM EST.  In that, If the agent has submitted a request after 2 PM of that day, Batch job should not count/increment the number field that day by the batch job. But should be included in next day's job. Assist me on this please.
sgsg
I have modifed the execute method like below, To capture the record which is submitted after 2pm EST On Monday and exclude it in Monday's 3pm EST batch run.
global void execute(Database.BatchableContext bc, List<TMA__c> tmas)
{
Datetime today = Datetime.now().date();
Datetime cutoffEnd= Datetime.newInstance(today.year(),today.month(),today.day(),14,0,0);
for (TMA__c tma : tmas) {
if(tma.status__c =='Submitted" && tma.submitteddate__c>cutoffEnd)
{  continue;  }

if (isBusinessHours(businessHoursId))
{ tma.numberField__c+=1;
}}
update tmas;
}

It excluded the 2 records as expected on Monday and updated all other records. But it should have updated these 2 records in yesterday's batch run , but it didn't. Batch updated all other records and excluded these 2 in yesterday's run which is not correct. What am I doing wrong? Please send some help here.