You need to sign in to do that
Don't have an account?
sg
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;
} }
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;
} }
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:
If this information helps, please mark the answer as best. Thank you
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.
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.