You need to sign in to do that
Don't have an account?
Roberto Albuquerque
How to write a Batchable Schedulable Test Class?
Hello everyone, I tried in several ways to create a test for the class below, but I am not succeeding, could anyone help me?
global class CancerDeColonBatch implements Schedulable, Database.Batchable<sObject> { String query; global CancerDeColonBatch() { query = 'SELECT Id FROM Account WHERE RecordTypeId IN (SELECT Id FROM RecordType WHERE Name = \'Paciente\' AND SObjectType = \'Account\') AND Detecta__pc = TRUE AND Idade__pc >= 21 AND Idade__pc <= 65'; system.debug(query); } global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<sObject> scope) { List<Account> accounts = (List<Account>) scope; Set<Id> accountIds = new Set<Id>(); for(Account c : accounts){ accountIds.add(c.Id); } List<AggregateResult> procedimentos = [SELECT C_digo_do_Procedimento__r.Name exame, Nome_do_Paciente__c conta, MAX(Data_de_Processamento__c) dataExame FROM Dados_Utiliza_o__c WHERE C_digo_do_Procedimento__r.Name IN ('40303250', '40202690', '40202720', '40202739', '40202666', '40201082', '41001265', '40201090', '40201350', '40202135') AND Nome_do_Paciente__c IN :accountIds GROUP BY Nome_do_Paciente__c, C_digo_do_Procedimento__r.Name]; Map<Id, Boolean> criarCase = new Map<Id, Boolean>(); for(AggregateResult ex : procedimentos){ system.debug(ex); Date dataExame = (Date) ex.get('dataExame'); Integer difCalc = Date.today().year() - dataExame.year(); if((ex.get('exame') == '40303250') && (difCalc >= 1)){ criarCase.put((Id) ex.get('conta'), true); } else if(((ex.get('exame') == '40202690') || (ex.get('exame') == '40202720') || (ex.get('exame') == '40202739')) && difCalc >= 5){ criarCase.put((Id) ex.get('conta'), true); } else if(((ex.get('exame') == '40202666') || (ex.get('exame') == '40201082') || (ex.get('exame') == '41001265') || (ex.get('exame') == '40201090') || (ex.get('exame') == '40201350') || (ex.get('exame') == '40202135')) && difCalc >= 10){ criarCase.put((Id) ex.get('conta'), true); } } List<Case> newCases = new List<Case>(); for(Id cId : accountIds){ if(!criarCase.keySet().contains(cId) || criarCase.get(cId)){ Case newCase = new Case( Subject = 'CancerDeColon - Exames Pendentes', AccountId = cId, Description = 'Exames pendentes: Sangue oculto nas fezes, pesquisa imunológica (40303250), Retossigmoidoscopia flexível com biópsia e/ou citologia (40202690), Retossigmoidoscopia rígida com biópsia e/ou citologia (40202720), Retossigmoidoscopia rígida com polipectomia (40202739), Colonoscopia com biópsia e/ou citologia (40202666), Colonoscopia (inclui a retossigmoidoscopia) (40201082), TC - Colonoscopia virtual (41001265), Colonoscopia com magnificação (40201090), Colonoscopia com cromoscopia (40201350), Colonoscopia com magnificação e tatuagem (40202135)' ); newCases.add(newCase); } } try{ system.debug('Entrou no insert'); insert newCases; } catch(Exception e){ system.debug('Erro: ' + e.getMessage()); } } global void execute(SchedulableContext sc) { system.debug('colon'); CancerDeColonBatch b = new CancerDeColonBatch(); database.executebatch(b); } global void finish(Database.BatchableContext BC) { } }
You can find the solution to your problem in the below link:
https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_batch
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi