how to write a batch apex to get notification when a managed package is getting expired soon and also we have to send a email notification when package is about to expire , I have written a batch but it is working as expected , can you help me with the logic
i was expecting that the batch should a email notification but it was not happening as excepted and instead it was giving me a error (First_Exception:Invalid token[)
This is the class
global class PackageExpiryNotificationBatch implements Database.Batchable<SObject>, Database.Stateful, Schedulable, Database.AllowsCallouts {
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id, Name, Expiration_Date__c FROM Package__c WHERE Expiration_Date__c <= :Date.today().addMonths(3)';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Package__c> scope) {
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
List<Package__c> packagesExpiring = new List<Package__c>();
// Fetch recipient email from custom setting
NotificationSettings__c settings = NotificationSettings__c.getValues('Default');
String recipientEmail = settings != null ? settings.RecipientEmail__c : '[email protected]';
for (Package__c pkg : scope) {
// Add to list for Mattermost notification
packagesExpiring.add(pkg);
// Create email notification
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
email.setToAddresses(new String[] {recipientEmail});
email.setSubject('Package Expiry Notification');
email.setPlainTextBody('The package ' + pkg.Name + ' is expiring on ' + pkg.Expiration_Date__c.format() + '.');
emails.add(email);
}
// Send all emails
if (!emails.isEmpty()) {
Messaging.sendEmail(emails);
}
// Send Mattermost notification
sendMattermostNotification(packagesExpiring, recipientEmail);
}
global void finish(Database.BatchableContext BC) {
// Log or perform any necessary post-batch operations
}
global void execute(SchedulableContext SC) {
Database.executeBatch(new PackageExpiryNotificationBatch());
}
// Method to send Mattermost notification
private void sendMattermostNotification(List<Package__c> packagesExpiring, String recipientEmail) {
String mattermostWebhookUrl = 'https://mattermost.example.com/hooks/your-webhook-url';
String message = 'The following packages are expiring within the next 3 months:n';
if (packagesExpiring.isEmpty()) {
message += 'No packages are expiring within the next 3 months.';
} else {
for (Package__c pkg : packagesExpiring) {
message += '- ' + pkg.Name + ' expiring on ' + pkg.Expiration_Date__c.format() + 'n';
}
}
HttpRequest req = new HttpRequest();
req.setEndpoint(mattermostWebhookUrl);
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setBody('{"text":"' + message + '"}');
Http http = new Http();
HttpResponse res = http.send(req);
if (res.getStatusCode() != 200) {
System.debug('Failed to send Mattermost notification: ' + res.getStatus());
}
}
}
Jay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.