You need to sign in to do that
Don't have an account?
SF Beginner 2019
calling a class to make it batch apex class/ job
How to call this apex class to make it a batch job?
public class ApplicationYearsinService{ public static void YearsinService(List<Application__c> appList){ //Declares the Variables, List, ID to be used Date dat = System.today(); Integer yr = dat.year(); Map<Id, Set<Integer>> setofYear = new Map<Id, Set<Integer>>(); Set<Id> accountIdSet = new set<Id>(); //Gets the values of the Account(Account__c from the Application for(Application__c cs: appList ){ accountIdSet.add(cs.Account__c); } //Query in the Application to get the value from the Account for(Application__c p : [SELECT ID, Account__c, Received_Date__c from Application__c where Account__c IN : accountIdSet ]){ if(p.Received_Date__c != null){ if(setofYear.containskey(p.Account__c)){ if(p.Received_Date__c != NULL) { setofYear.get(p.Account__c).add(p.Received_Date__c.Year()); } } else { setofYear.put(p.Account__c, new Set<Integer>{p.Received_Date__c.Year()}); } } } Map<String, List<Application__c>> appMap = new Map<String ,List<Application__c>>(); Map<String, Account> lisAcct = new Map<String, Account>(); List<Account> acctToUpdate = new List<Account>(); List<Account> accountListToLoop = [Select Id, YearsinService__c, (Select Id FROM Applications__r LIMIT 1) from Account where Id=:accountIdSet]; for(Account acc: accountListToLoop){ lisAcct.put(acc.Id+'', acc); } for(Account act : accountListToLoop ){ if(setofYear.containskey(act.id)){ act.YearsinService__c = setofYear.get(act.id).size(); acctToUpdate.add(act); lisAcct.remove(act.Id+''); } else { act.YearsinService__c = 0; acctToUpdate.add(act); } } Database.upsert(acctToUpdate, false); } }
Write below scheduler for call ApplicationYearsinService class to make it a batch job. And Instead of params pass Application__c list.
global with sharing class ApplicationYearsinService_Scheduler implements Schedulable {
Public static void SchedulerMethod() {
string timeinterval = '0 0 0 1/1 ? ';
System.schedule('Batch Class will run every day at 12:00 AM',timeinterval, new ApplicationYearsinService_Scheduler());
}
global void execute(SchedulableContext SC) {
ApplicationYearsinService.YearsinService(params);
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
Write below scheduler for call ApplicationYearsinService class to make it a batch job. And Instead of params pass Application__c list.
global with sharing class ApplicationYearsinService_Scheduler implements Schedulable {
Public static void SchedulerMethod() {
string timeinterval = '0 0 0 1/1 ? ';
System.schedule('Batch Class will run every day at 12:00 AM',timeinterval, new ApplicationYearsinService_Scheduler());
}
global void execute(SchedulableContext SC) {
ApplicationYearsinService.YearsinService(params);
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
what do you mean by pass the Application__c list? do I just need to change ApplicationYearsinService.YearsinService(Application__c); and how can I test this in developer console?
Regards
global with sharing class ApplicationYearsinService_Scheduler implements Schedulable {
Public static void SchedulerMethod() {
string timeinterval = '0 0 11 1/1 ? ';
System.schedule('Batch Class will run every day at 12:00 AM',timeinterval, new ApplicationYearsinService_Scheduler());
}
global void execute(SchedulableContext SC) {
List<Application__c> appList = new List<Application__c>();
ApplicationYearsinService.YearsinService(appList);
}
}
Run in the Execute Anonymous
ApplicationYearsinService_Scheduler cls = new ApplicationYearsinService_Scheduler();
cls.execute(null);
system.debug('cls' + cls);