Use Case:

Create a batch class which would:

  1. Create a Task to review opportunity for all the opportunities irrespective of their StageName.
  2. Tasks should be assigned to the related Opportunity Owner and if the any of the Owner/User is inactive then assign the task to any user with profile System Administrator.
global class BatchTasksCreation implements Database.Batchable<sObject> {
// Get all the opportunities in Database.QueryLocator
global Database.QueryLocator start(Database.BatchableContext bc) {
String query = 'SELECT Id, OwnerId, Owner.IsActive FROM Opportunity';
return Database.getQueryLocator(query);
}
// Execute the logic for opportunities quried in start method in batches
global void execute(Database.BatchableContext bc, List<Opportunity> scope) {
List<Task> listOfNewTasks = new List<Task>();
User adminUser = [Select Id From User Where Profile.Name = 'System Administrator' LIMIT 1];
for(Opportunity opp : scope) {
String owner;
// If the owner is active then set the opportunity owner Id in the "owner" variable else set Admin Id
if(opp.Owner.IsActive) {
listOfNewTasks.add(new Task(WhatId = opp.Id,
OwnerId = opp.OwnerId,
Subject = 'Please review the related opportunity.'));
}
else {
listOfNewTasks.add(new Task(WhatId = opp.Id,
OwnerId = adminUser.Id,
Subject = 'Please review the related opportunity.'));
}
}
insert listOfNewTasks;
}
global void finish(Database.BatchableContext bc) {
}
}

Test Class:

Verify that when batch class is run,

Scenario 1: Task should be created for each Opportunity and is assigned to the Opportunity Owner.

Scenario 2: When any of the Owner is inactive for any Opportunity, Task created for that Opportunity should be assigned to any User having profile System Administrator.

// Insert 2 opportinities with different owners
// 1 with admin and the other one with new user
@isTest
private class BatchTasksCreationTest {
@testSetup
static void dataSetup() {
// Create opportunities
Opportunity op1 = new Opportunity(Name = 'Admin Opp',
StageName = 'Prospecting',
CloseDate = System.today());
insert op1;
Profile stanProfile = [Select Id From Profile Where Name = 'Standard User'];
User usr = new User(LastName = 'User12ka4',
FirstName = 'Test',
Alias = 'tusr',
Email = 'test.user12ka4@apexstepbystep.com',
Username = 'test.user12ka4@apexstepbystep.com',
ProfileId = stanProfile.id,
TimeZoneSidKey = 'GMT',
LanguageLocaleKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LocaleSidKey = 'en_US'
);
System.runAs(usr) {
Opportunity op2 = new Opportunity(Name = 'Standard Opp',
StageName = 'Prospecting',
CloseDate = System.today());
insert op2;
}
}
// Execute the batch class for tasks creation and verify that two tasks should be created
// 1st for admin opp with admin user
// 2nd for standard opp with standard user
    @isTest
static void testMethod1() {
Opportunity adminOpp = [Select Id, OwnerId, Owner.Name From Opportunity Where Name = 'Admin Opp'];
Opportunity standOpp = [Select Id, OwnerId, Owner.Name From Opportunity Where Name = 'Standard Opp'];
Test.startTest();
BatchTasksCreation bc = new BatchTasksCreation();
Database.executeBatch(bc, 10);
Test.stopTest();
// Assert that two tasks should be created 1 for each opportunity
List<Task> tasks = [Select Id, OwnerId, WhatId From Task];
System.assertEquals(2, tasks.size(), '2 Tasks should be created.');
System.assertEquals(1, [Select Id From Task Where WhatId = :adminOpp.Id AND OwnerId = :adminOpp.OwnerId].size(), 'Task is not created for related Opportunity.');
System.assertEquals(1, [Select Id From Task Where WhatId = :standOpp.Id AND OwnerId = :standOpp.OwnerId].size(), 'Task is not created for related Opportunity.');
}
// Deactivate the user and then run the Batch class,
// assert that 2 tasks should be created, 1st for admin opp and 2nd for standard opp
// and owner of both the tasks should be same as admin opp
@isTest
static void testMethod2() {
Opportunity adminOpp = [Select Id, OwnerId From Opportunity Where Name = 'Admin Opp'];
Opportunity standOpp = [Select Id, OwnerId From Opportunity Where Name = 'Standard Opp'];
User standUser = [Select Id, Name, IsActive From User Where Username = 'test.user12ka4@apexstepbystep.com'];
standUser.IsActive = false;
update standUser;
Test.startTest();
BatchTasksCreation bc = new BatchTasksCreation();
Database.executeBatch(bc, 10);
Test.stopTest();
List<Task> tasks = [Select Id, OwnerId, WhatId From Task];
System.assertEquals(2, tasks.size(), '2 Tasks should be created.');
System.assertEquals(1, [Select Id From Task Where WhatId = :adminOpp.Id AND OwnerId = :adminOpp.OwnerId].size(), 'Task is not created for related Opportunity.');
System.assertEquals(1, [Select Id From Task Where WhatId = :standOpp.Id AND OwnerId = :adminOpp.OwnerId].size(), 'Task is not created for related Opportunity.');
}
}
Code Coverage: 100%
Like it ? Share : Do Nothing

Leave a Reply