-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniversalQueueTrigger.tgr
30 lines (26 loc) · 1.34 KB
/
UniversalQueueTrigger.tgr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
trigger UniversalQueueTrigger on Universal_Queue__c (after insert) {
//static Boolean hasBatchStarted = false;
// Check if the trigger is running in a non-test context
if (!Test.isRunningTest()) {
// Retrieve the custom setting
BatchJobStatus__c batchJobStatus = BatchJobStatus__c.getInstance('Universal');
// If the custom setting doesn't exist, create it
if (batchJobStatus == null) {
batchJobStatus = new BatchJobStatus__c(Name = 'Universal');
insert batchJobStatus;
}
// Check if there's an existing batch job for the QueueProcessor class
Integer activeBatchJobs = [SELECT COUNT() FROM AsyncApexJob WHERE JobType = 'BatchApex'
AND ApexClass.Name = 'QueueProcessor' AND Status IN ('Processing', 'Preparing')];
// If there are no active batch jobs, start a new one
if (!batchJobStatus.BatchJobStarted__c) {
QueueProcessor batchJob = new QueueProcessor();
Database.executeBatch(batchJob);
batchJobStatus.BatchJobStarted__c = true;
update batchJobStatus;
} else {
// Log or handle the scenario where a batch job is already in progress
System.debug('A batch job is already in progress for QueueProcessor class. Skipping new job.');
}
}
}