Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Account_CloneCasesOnAnniversary implements Database.Batchable<SObject>
- {
- String errors = '';
- integer todayday = date.today().day();
- Integer currentmonth = date.today().month();
- public Database.QueryLocator start(Database.BatchableContext bc) {
- return Database.getQueryLocator('SELECT Anniversary__c, NSCHBC__c, Status__c, ID, (SELECT Id, Anniversary__c, OwnerId, ContactId, ContactEmail, ContactPhone, Description, Priority, RecordTypeId, Subject FROM cases order by CreatedDate) from Account WHERE DAY_IN_MONTH(Anniversary__c) =:todayday AND CALENDAR_MONTH(Anniversary__c ) =: currentmonth');
- }
- public void execute(Database.BatchableContext bc, List<Account> scope) {
- Id Activation_Action_Plan_RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Activation Action Plan').getRecordTypeId();
- Id Master_Action_Plan_RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Master Action Plan').getRecordTypeId();
- Id NSCHBC_Action_Plan_RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('NSCHBC Action Plan').getRecordTypeId();
- Id Total_Core_Action_Plan_RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Total Core Action Plan').getRecordTypeId();
- if(scope.isEmpty()) return;
- Map<Id, Case> clonedCasesMap = new Map<Id,Case>();
- Map<Id, list<Case>> NSCHBCAccountToCasesMap = new Map<Id, list<Case>>();
- for(Account account : scope) {
- // Clone cases on Anniversary
- if(account.cases!=null && account.cases.size() > 0) {
- list<Case> lstCase = new list<Case>();
- for(Case objcase : account.cases){
- // Fetch only those Cases to be cloned whose Record type is of
- if(objcase.RecordTypeId == Activation_Action_Plan_RecordTypeId
- || objcase.RecordTypeId == Master_Action_Plan_RecordTypeId
- || objcase.RecordTypeId ==NSCHBC_Action_Plan_RecordTypeId
- || objcase.RecordTypeId ==Total_Core_Action_Plan_RecordTypeId){
- Case cloneCase = new Case();
- cloneCase = objcase.clone();
- clonedCasesMap.put(objcase.Id,cloneCase);
- }
- }
- }
- // Check whether Account is of NSCHBS type
- if(account.NSCHBC__c && account.Status__c == 'Active') {
- NSCHBCAccountToCasesMap.put(account.Id, account.cases);
- }
- system.debug('--- NSCHBCAccountToCasesMap --->' + NSCHBCAccountToCasesMap);
- }
- if(!clonedCasesMap.isEmpty()) {
- Database.SaveResult[] srList = Database.insert(clonedCasesMap.values(), false);
- Integer index = 0;
- for(Database.SaveResult result : srList ) {
- if(!result.isSuccess()) {
- String errMsg = result.getErrors()[0].getMessage();
- }
- index++;
- }
- }
- // create task on cases
- List<Task> taskCreateList = new List<Task>();
- if(!NSCHBCAccountToCasesMap.isEmpty()) {
- for(Id accountId : NSCHBCAccountToCasesMap.keyset()){
- if(NSCHBCAccountToCasesMap.get(accountId) != null && NSCHBCAccountToCasesMap.get(accountId).size() > 0) {
- for(Case caseObj : NSCHBCAccountToCasesMap.get(accountId)) {
- Task createTask = new Task();
- createTask.subject = 'QCR Anniversary';
- createTask.ActivityDate = caseObj.Anniversary__c + 330;
- createTask.OwnerId = caseObj.OwnerId;
- taskCreateList.add(createTask);
- }
- }
- }
- }
- // check for task list is empty or not
- if(!taskCreateList.isEmpty()) {
- // insert task list Of Task which were cloned
- Database.SaveResult[] srListTask = Database.insert(taskCreateList, false);
- Integer indexTask = 0;
- System.debug('Created Task is' +srListTask);
- for(Database.SaveResult resultTask : srListTask ) {
- if(!resultTask.isSuccess()) {
- String errMsgTask = resultTask.getErrors()[0].getMessage();
- }
- indexTask++;
- }
- }
- }
- public void finish(Database.BatchableContext context) {
- }
- }
- @isTest
- private class Account_CloneCasesOnAnniversaryTest {
- static testmethod void test() {
- Account objAcc = new Account ();
- objAcc.name = 'TestClass';
- objAcc.Anniversary__c = date.today();
- objAcc.NSCHBC__c=true;
- objAcc.Status__c='Active';
- insert objAcc;
- Id Total_Core_Action_Plan_RecordTypeId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Total Core Action Plan').getRecordTypeId();
- Case objCase = new Case();
- objCase.AccountId = objAcc.Id;
- objCase.RecordTypeId=Total_Core_Action_Plan_RecordTypeId;
- insert objCase;
- Task objTask = new Task();
- ObjTask.WhatId = ObjCase.Id;
- insert objTask;
- Test.startTest();
- Account_CloneCasesOnAnniversary c = new Account_CloneCasesOnAnniversary();
- Database.executeBatch(c);
- Test.stopTest();
- // Verify merchandise items got deleted
- Integer i = [SELECT COUNT() FROM Case];
- System.assertEquals(i, 2);
- Integer j = [select count() from Task];
- system.assertEquals(j,2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement